您的位置:首页 > 其它

Audio and Video

2017-01-08 16:31 295 查看

Audio and Video

Quickview

Audio playback and record
Video playback
Handles data from raw resources, files, streams
Built-in codecs for a variety of media. See
Android Supported Media Formats    

In this document

Audio and Video Playback
Playing from a Raw Resource

Playing from a File or Stream

Playing JET Content


Audio Capture

Key classes

MediaPlayer

MediaRecorder

JetPlayer

SoundPool

See also

Data Storage
JetCreator User Manual

The Android platform offers built-in encoding/decoding for a variety of common media types, so that you can easily integrate audio, video, and images into yourapplications. Accessing the platform's media capabilities is fairly straightforward — you do so
using the same intents and activities mechanism that the rest ofAndroid uses.

Android lets you play audio and video from several types of data sources. Youcan play audio or video from media files stored in the application's resources(raw resources), from standalone files in the filesystem, or from a data streamarriving over a network
connection. To play audio or video from yourapplication, use the
MediaPlayer
class.

The platform also lets you record audio and video, where supported by themobile device hardware. To record audio or video, use the
MediaRecorder
class. Note that the emulator doesn't have hardwareto capture audio or video, but actual mobile devices are likely to provide thesecapabilities,
accessible through the MediaRecorder class.

For a list of media formats for which Android offers built-in support,see the
Android MediaFormats appendix.

(     Android平台提供内置的解码和编码功能给通用的媒体类型,所以 你可以很容易的将音频、视频和图片整合到你的应用。通过借助平台的媒体处理的性能是非常直接了当的-使用Android的其余部分,如相同的Intent和Activity的机制。

       Android是你能播放各种类型的资源的音频和视频。你可以播放保存在应用的资源(raw resource)、保存在文件系统的单独文件、或者网络数据流(URL)。使用MediaPlay类去播放音频和视频。

      Android平台也让你录音和录视频,但需要手机硬件支持。使用MediaRecorder类录音和录视频。注意,模拟器是没有硬件支持录视频和录音的,但是真机都会具备这样的能力。

      关于Android内置的功能支持的媒体类型可以查看the
Android MediaFormats 附录)

Audio and Video Playback(后台播放音频和视频)

Media can be played from anywhere: from a raw resource, from a file from the system, or from an available network (URL).

You can play back the audio data only to the standardoutput device; currently, that is the mobile device speaker or Bluetooth headset. Youcannot play sound files in the conversation audio.

Playing from a Raw Resource(播放raw媒体资源)

Perhaps the most common thing to want to do is play back media (notably sound)within your own applications. Doing this is easy:

(可能做得最多的是你的应用在后台播放媒体,尤其是音频,做到这个很简单)

Put the sound (or other media resource) file into the
res/raw
folder of your project, where the Eclipse plugin (or aapt) will find it and make it into a resource that can be referenced from your R class
Create an instance of
MediaPlayer
, referencing that resource using
MediaPlayer.create
, and then call
start()
on the instance:
    MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
    mp.start();

To stop playback, call
stop()
. If you wish to later replay the media, then you must
reset()
and
prepare()

the MediaPlayer objectbefore calling
start()
again. (
create()
calls
prepare()
the first time.)

(需要停止后台播放时,调用stop()。如果你想重新播放这首歌,必须在调用start()前调用reset() 和 prepare()。create()方法中会首次调用prepare())

To pause playback, call
pause()
. Resume playback from where you paused with
start()
.

(需要暂停后台播放时,调用pause()。从你暂停处继续播放音频)

Playing from a File or Stream(从文件系统或者数据流读取数据并播放)

You can play back media files from the filesystem or a web URL:

Create an instance of the
MediaPlayer
using
new

Call
setDataSource()
with a String containing the path (local filesystem or URL) to the
file you want to play
First
prepare()
then
start()
on the instance:
    MediaPlayer mp = new MediaPlayer();
    mp.setDataSource(PATH_TO_FILE);
    mp.prepare();
    mp.start();

stop()
and
pause()
work the same as discussed above.

(stop()和pause()的调用和上面讨论的一致)

Note: It is possible that
mp
could be null, so good code should
null
check after the
new
. Also,
IllegalArgumentException
and
IOException
either need to be caught or passed on when using
setDataSource()
, since the file you are referencing may not exist.
(注意:创建的MediaPlayer可能是null,所以好的代码需要做判空操作。
IllegalArgumentException
and
IOException
需要传递传递出去或者处理掉在调用setDataSource()的时候,因为你引用的文件可能不存在)

Note:If you're passing a URL to an online media file, the file must be capable of progressive download.
(注意:如果你使用一个指向在媒体文件的URL作为参数,该文件必须是支持渐进式下载的)

Playing JET content(该段翻译请看该链接http://blog.sina.com.cn/s/blog_48d491300100ztm9.html

The Android platform includes a JET engine that lets you add interactive playback of JET audio content in your applications. You can create JET content for interactive playback using the JetCreator authoring application that ships with the SDK. To play and
manage JET content from your application, use the
JetPlayer
class.

For a description of JET concepts and instructions on how to use the JetCreator authoring tool, see the

JetCreator User Manual. The tool is available fully-featured on the OS X and Windows platforms and the Linux version supports all the content creation features, but not the auditioning of the imported assets.

Here's an example of how to set up JET playback from a .jet file stored on the SD card:

JetPlayer myJet = JetPlayer.getJetPlayer();
myJet.loadJetFile("/sdcard/level1.jet");
byte segmentId = 0;

// queue segment 5, repeat once, use General MIDI, transpose by -1 octave
myJet.queueJetSegment(5, -1, 1, -1, 0, segmentId++);
// queue segment 2
myJet.queueJetSegment(2, -1, 0, 0, 0, segmentId++);

myJet.play();

The SDK includes an example application — JetBoy — that shows how to use
JetPlayer
to create an interactive music soundtrack in your game. It also illustrates how to use JET events to synchronize music and game logic. The application
is located at
<sdk>/platforms/android-1.5/samples/JetBoy
.

Audio Capture(录音)

Audio capture from the device is a bit more complicated than audio/video playback, but still fairly simple:

(通过设备录音是比音频/视频播放)

Create a new instance of
android.media.MediaRecorder
using
new

Create a new instance of
android.content.ContentValues
and put in some standard properties like
TITLE
,
TIMESTAMP
, and the all important
MIME_TYPE

Create a file path for the data to go to (you can use
android.content.ContentResolver
to create an entry in the Content database and get it to
assign a path automatically which you can then use)
Set the audio source using
MediaRecorder.setAudioSource()
. You will probably want to use
MediaRecorder.AudioSource.MIC

Set output file format using
MediaRecorder.setOutputFormat()


Set the audio encoder using
MediaRecorder.setAudioEncoder()


Call
prepare()
on the MediaRecorder instance.
To start audio capture, call
start()
.

To stop audio capture, call
stop()
.

When you are done with the MediaRecorder instance, call
release()
on it.

Example: Audio Capture Setup and Start

The example below illustrates how to set up, then start audio capture.

    recorder = new MediaRecorder();
    ContentValues values = new ContentValues(3);

    values.put(MediaStore.MediaColumns.TITLE, SOME_NAME_HERE);
    values.put(MediaStore.MediaColumns.TIMESTAMP, System.currentTimeMillis());
    values.put(MediaStore.MediaColumns.MIME_TYPE, recorder.getMimeContentType());
   
    ContentResolver contentResolver = new ContentResolver();
   
    Uri base = MediaStore.Audio.INTERNAL_CONTENT_URI;
    Uri newUri = contentResolver.insert(base, values);
   
    if (newUri == null) {
        // need to handle exception here - we were not able to create a new
        // content entry
    }
   
    String path = contentResolver.getDataFilePath(newUri);

    // could use setPreviewDisplay() to display a preview to suitable View here
   
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    recorder.setOutputFile(path);
   
    recorder.prepare();
    recorder.start();

Stop Recording

Based on the example above, here's how you would stop audio capture.

    recorder.stop();
    recorder.release();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: