How to Resume The Mediaplayer

How to resume the mediaplayer?

Thank you for your attention but I've got it myself

for pausing the Mediaplayer I used:

Mediaplayer.pause();
length=Mediaplayer.getCurrentPosition();

and for resuming the player from the position where it stopped lately is done by:

Mediaplayer.seekTo(length);
Mediaplayer.start();

How to resume MediaPlayer in Android after pressing the home button and reopen the app

I understand you have accepted an answer but I'll explain what the problem was based on your comments on your question and the comments from you and Nana on Nana's answer.

  1. A SurfaceView is destroyed when it is no longer visible and recreated when it becomes visible again.
  2. You are calling mp.pause() in your Activity onPause() method.
  3. When you go to the 'recent' apps list and attempt to restart the app, the SurfaceView is recreated.
  4. In surfaceCreated method you are calling mp.prepare() but at this point mp is in a paused state and calling prepare() on a paused Mediaplayer will throw an IllegalStateException.
  5. You have a try / catch block which catches IllegalStateException and calls finish() - this is why the first attempt to restart the app from the 'recent' list causes the the Activity to be destroyed.
  6. As a result of the above sequence of events you need a second attempt to re-open the app from the 'recent' list but as the Activity has been destroyed it will go through a full creation (with onCreate(..) being called. This is why it starts from the beginning.

The answer from Nana is a workaround but it will still mean having to use two attempts to restart your Activity from the 'recent' list.

Unfortunately the MediaPlayer class is lacking in methods for checking the 'state' with isPlaying being the only useful method for any state. It's a shame the class devs didn't think to add a getState() method (or similar) to allow checking for whether it's started, playing, stopped, paused etc.

One way of preventing the IllegalStateException would be to have a boolean called isPaused (for example) then modify your Activity onPause() as follows...

if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
isPaused = true;
}

...and in surfaceCreated(...)...

try {
if (isPaused) {
mpStart();
isPaused = false;
}
else
mediaPlayer.prepare();
}
// catch blocks here

Resume mediaplayer after paused

i can manage the three times click on same image correctly as below:

1-First click ==) play MP3.

1-Second click ==) pause mp3

3-Third click ==) resume playing again the mp3 from where it paused .

 if (mViewPager.getCurrentItem() == 0) {
if(mp != null && mp.isPlaying()){
mp.pause();
length = mp.getCurrentPosition();

}else{

mp = MediaPlayer.create(MainActivity.this, R.raw.aa);
mp.seekTo(length);

mp.start();
}

}

How to resume MediaPlayer when paused

U seem to have put the statements in the wrong sequence, try this instead:

music.seekTo(length);
music.start();

MediaPlayer mp3 won't resume after pause, known solutions not working

You have to "pause"(not stop) the mediaplayer in onPause, then start in onResume. That worked for me.



Related Topics



Leave a reply



Submit