Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to pause and resume VideoView with MediaPlayer in activity onPause() and onResume() methods, but in onResume() method MediaPlayer throws java.lang.IllegalStateException. I didn't release MediaPlayer but I think MediaPlayer automatically released after activity paused. How should I handle it?

private MediaPlayer mediaPlayer;
void prepareVideo() {
    videoView = new VideoView(context.getApplicationContext());
    String path = "android.resource://" + getPackageName() + "/" + 
    R.raw.my_video;
    videoView.setVideoPath(path);
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
    @Override
    public void onPrepared(MediaPlayer mp) {
        mediaPlayer = mp;
        mediaPlayer.start();
@Override
protected void onResume() {
    super.onResume();
    if (mediaPlayer != null) {
        mediaPlayer.start();
@Override
protected void onPause() {
    if (mediaPlayer != null && mediaPlayer.isPlaying()) {
        mediaPlayer.pause();
    super.onPause();

The exception:

    Caused by: java.lang.IllegalStateException
    at android.media.MediaPlayer._start(Native Method)
    at android.media.MediaPlayer.start(MediaPlayer.java:1194)
    at co.myapp.app.reborn.myappTestActivity.onResume(myappTestActivity.java:370)
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1259)
    at android.app.Activity.performResume(Activity.java:6347)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3110)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3152) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1400) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5530) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:734) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624) 

Take a look at the mediaplayer state diagram on android documentation

MediaPlayer state diagram

According to the diagram you have to call setDataSource() and prepare() before calling start().

Probably something wrong happened before. Your logcat should point you in the right direction.

My guess is that your mediaplayer is not in paused state but in stopped state. So you have to call prepare and then start, not just start.

Unfortunately in this way your playback will restart from scratch.

You can use seek command for resuming a position saved during the activity pausing.

I'm using VideoView, I never use setDataSource(currentConext, url), this method is called internally in VideoView class. – Shervin Gharib Jun 25, 2018 at 11:48

We just need to implement MediaPlayer.OnSeekCompleteListener interface and set MediaPlayer in onSeekComplete method.

private MediaPlayer mediaPlayer;
@Override
public void onSeekComplete(final MediaPlayer mp) {
    mediaPlayer = mp;
                I don't remember but it was working. probably by trial and error. Is there any problem with this approach? @VadimKotov
– Shervin Gharib
                Nov 19, 2020 at 2:15

I was facing this issue yesterday and I'd like to share my experience facing this issue, the root cause and the fix in my particular issue; this might be helpful for someone else.

This is what I found in my particular issue.

  • I´m running MediaPlayer in Fragment
  • When the phone goes to sleep (black screen), in the fragment-life-cycle the stop() function is call.
  • MediaPlayer recommends to release() MediaPlayer resources on STOP state.
  • When the phone resumes, it complains with illegal-state-exception because there are no MediaPlayer resource available, remember it was released in the STOPE state.
  • So, to fix it. I overrided start() state and I got another instance of MediaPlayer if it was null at that specific point in time. START state is called at resume time.

    sample code

    public class xMediaPlayer extends MediaPlayer
        private static xMediaPlayer instance = null;
        public static xMediaPlayer getInstance( )
            if( instance == null )
                instance = new xMediaPlayer( );
            return instance;
    

    Override start() on fragment

    @Override
    public void onStart() {
        super.onStart();      
        mMediaPlayer = xMediaPlayer.getInstance( );
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.