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.
- A
SurfaceView
is destroyed when it is no longer visible and recreated when it becomes visible again. - You are calling
mp.pause()
in yourActivity
onPause()
method. - When you go to the 'recent' apps list and attempt to restart the app, the
SurfaceView
is recreated. - In
surfaceCreated
method you are callingmp.prepare()
but at this pointmp
is in a paused state and callingprepare()
on a pausedMediaplayer
will throw anIllegalStateException
. - You have a
try / catch
block which catchesIllegalStateException
and callsfinish()
- this is why the first attempt to restart the app from the 'recent' list causes the theActivity
to be destroyed. - 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 (withonCreate(..)
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
How to Get Data from Dialogfragment to a Fragment
Disable Webview Touch Events in Android
How to Click on an Item Inside a Recyclerview in Espresso
How to Run a Specific Android App Using Terminal
Easier Way to Get View's Id (String) by Its Id (Int)
Using Onbackpressed() in Android Fragments
Commitallowingstateloss() in Fragment Activities
Check If a Vpn Connection Is Active in Android
How to Make a Linearlayout Scrollable
Fragment Standard Transition Not Animating
How to Set The Rounded Corner Radius of a Color Drawable Using Xml
How to Set The App Icon as The Notification Icon in The Notification Drawer
How to List All Files and Folders Locating on Sd Card
Apply Two Different Font Styles to a Textview
How to Enable Autostart Option for My App in Xiaomi Phone Security App Programmatically in Android