Chinaunix首页 | 论坛 | 博客
  • 博客访问: 15336873
  • 博文数量: 2005
  • 博客积分: 11986
  • 博客等级: 上将
  • 技术积分: 22535
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-17 13:56
文章分类

全部博文(2005)

文章存档

2014年(2)

2013年(2)

2012年(16)

2011年(66)

2010年(368)

2009年(743)

2008年(491)

2007年(317)

分类:

2010-09-29 19:42:13

Android support Media API as optional APIs. The media APIs provide functionality of playing and recording audio files and video files. In today’s post we will see how to play and record audio files.

Playing Audio File:
Android has defined a MediaPlayer class for playing media file. This class can be used to play the audio files. The class provides static methods to create the instance of the media player. Here is how the MediaPlayer object can be obtained using create methods

MediaPlayer.create(context, Uri.parse("file:///sdcard/audio_file.mp3");

and

MediaPlayer.create(context, R.raw.song_file);

The first create methods gets the Uri for the audio file and can be used to play files present on the phone sdcard. The second create method use the application resource file song_file.mp3. The song_file.mp3 file is added as a raw resource under res/raw directory.
Note that if the MediaPlayer object does not get created successfully, the create methods will return null so it will be better to check for the return object for null value to avoid NullPointer exception.

The MediaPlayer object can also be obtained by instantiating using MediaPlayer constructor. The object created like this has to be initialized with the file that needs to be played. setDataSource() method can be used to initialize the object with the audio file.

MediaPlayer mediaPlayer = new MediaPlayer();

try {
mediaPlayer.setDataSource("/sdcard/audio_file.mp3"); // provide path to the file.
} catch (IllegalArgumentException e) {
// handle exception
} catch (IllegalStateException e) {
// handle exception
} catch (IOException e) {
// handle exception
}

If you want to send the media player object with one of the files from application raw resources or from application assets files you can do that as follows:

try {
AssetFileDescriptor fd = getResources().openRawResouceFd(R.raw.song_file);
mediaPlayer.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getLength());
fd.close();
} catch (IllegalArgumentException e) {
// handle exception
} catch (IllegalStateException e) {
// handle exception
} catch (IOException e) {
// handle exception
}

The MediaPlayer object has to be prepared before the player can start playing the song. The prepare() method is the blocking method and blocks until the media player is ready to play the song. One non blocking method prepareAsync() is also provided. The non blocking prepare method should be used in case media player is used to play a song from a stream and need to buffer data before song can be played. The create methods that we saw earlier call the prepare() method and application must not call it again. To start file playback use start() method. This is how you play a file:

MediaPlayer mediaPlayer = new MediaPlayer();

try {
mediaPlayer.setDataSource("/sdcard/audio_file.mp3"); // provide path to the file.
mediaPlayer.prepare();
} catch (IllegalArgumentException e) {
// handle exception
} catch (IllegalStateException e) {
// handle exception
} catch (IOException e) {
// handle exception
}

mediaPlayer.start();

The file playback can be pause with pause() method, and can be resumes with start method. The file playback can be stopped with stop() method. The media player object has to reset before it can be setup for some other song file. The media player object has to be released after its use.

Android Media API defines some listeners like OnCompletionListener, OnPrepareListener, OnErrorListener, OnBufferingUpdateListener
OnCompletionListener event gets fired when media player complete the playing of the current audio file. You can use this listener event for playing next song from the list or releasing the media player object.

OnPrepareListener is needed when prepareAsync method is used. Since the prepareAsync method does not block for the call duration, the application can not start playing the song. The onPrepared() event function will be called once the media player is ready. You can start playing the song in the onPrepared() method.

OnErrorListener is also used in case of asynchronous operations. If any error occurs in asynchronous operation, onError() method of this listener will be called.

Recording Audio Files:

The Media API also provides functionality to record audio files. The MediaRecorder class can be used for media recording. The MediaRecorder has to be initialized with the Audio Encoder, output format (codec to be used for recorded file), Audio Source (like Microphone) and output file path where the recorded file will be stored. Here is how recording can be done:

MediaRecorder recorder = new MediaRecorder();

recorder.setAudioSource(MediaRecorder.AudioSource.MIC); recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

recorder.setOutputFile(filePath); // complete file path

recorder.prepare(); recorder.start();

This will start the recording the recording can be stored with stop() method. The MediaRecorder object also needs to be release after its use.
That all for today’s post. We will see Media APIs Video support in some other post.

阅读(3966) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~