Android Media Player play/pause Button

Dray picture Dray · Feb 27, 2012 · Viewed 88.5k times · Source

In my project, I am playing music file in android media player by using the following code

MediaPlayer mPlayer = MediaPlayer.create(MyActivity.this, R.raw.myfile);
mPlayer.start();

the above is coded in the onclick of the play button. I want to pause the playback by clicking the same button again.ie) single button for play/pause. How shall i do this.

Answer

Ruuhkis picture Ruuhkis · Feb 27, 2012

You could use simple if-check to handle the pausing. Try this:

if(mPlayer.isPlaying()){
    mPlayer.pause();
} else {
    mPlayer.start();
}