Playing an audio file with Android
Posted by Marco Dinacci on 0 commentsUpdate: The complete source code for this demo is available for download.
Playing an audio file is as easy as setting up a MediaPlayer and a MediaController.
First create a new activity that implements the MediaPlayerControl interface.
public class PlayAudioActivity extends Activity implements MediaPlayerControl {
private MediaController mMediaController;
private MediaPlayer mMediaPlayer;
private Handler mHandler = new Handler();
In the onCreate method we create and configure a MediaPlayer and a MediaController. The first is the object that perform the typical operations on an audio file like playing, pausing and seeking. The second is a view containing the buttons that launch the just mentioned operations through our MediaPlayerControl class. Let's see the onCreate code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mMediaPlayer = new MediaPlayer();
mMediaController = new MediaController(this);
mMediaController.setMediaPlayer(PlayAudioActivity.this);
mMediaController.setAnchorView(findViewById(R.id.audioView));
String audioFile = "" ;
try {
mMediaPlayer.setDataSource(audioFile);
mMediaPlayer.prepare();
} catch (IOException e) {
Log.e("PlayAudioDemo", "Could not open file " + audioFile + " for playback.", e);
}
mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mHandler.post(new Runnable() {
public void run() {
mMediaController.show(10000);
mMediaPlayer.start();
}
});
}
});
}
In addition to configuring our MediaController and MediaPlayer we create an anonymous OnPreparedListener in order to start the player only when the media source is ready for playback.
Remember to cleanup the media player when the Activity is destroyed.
@Override
protected void onDestroy() {
super.onDestroy();
mMediaPlayer.stop();
mMediaPlayer.release();
}
At last we implement the MediaPlayerControl interface. The code is very straightforward:
@Override
public boolean canPause() {
return true;
}
@Override
public boolean canSeekBackward() {
return false;
}
@Override
public boolean canSeekForward() {
return false;
}
@Override
public int getBufferPercentage() {
int percentage = (mMediaPlayer.getCurrentPosition() * 100) / mMediaPlayer.getDuration();
return percentage;
}
@Override
public int getCurrentPosition() {
return mMediaPlayer.getCurrentPosition();
}
@Override
public int getDuration() {
return mMediaPlayer.getDuration();
}
@Override
public boolean isPlaying() {
return mMediaPlayer.isPlaying();
}
@Override
public void pause() {
if(mMediaPlayer.isPlaying())
mMediaPlayer.pause();
}
@Override
public void seekTo(int pos) {
mMediaPlayer.seekTo(pos);
}
@Override
public void start() {
mMediaPlayer.start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mMediaController.show();
return false;
}
}
As a final touch we override the onTouchEvent in order to show the MediaController buttons when the user click on the screen.
Since we create our MediaController programmatically, the layout is very simple:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/audioView">
</LinearLayout>