Search code examples
androidvideomedia-playersurfaceview

Media player not resume correctly in android


I have used SurfaceView in my android application and play videos by mediaplayer on this SurfaceView.

In my activity I play, pause and resume video files successfully.

When I pause the video in my VideoActivity then open a new activity on it and then come back to my VideoActivity and resume the video, I hear the sound if the resumed video but have no display and the screen is black.

Why? How can I solve this problem?

My pause and resume methods:

public void pausePlayback() {

        if (mediaPlayer != null && mediaPlayer.isPlaying())
        {
            mediaPlayer.pause();
        }
    }

public void resumePlayback() {

        if (mediaPlayer != null && !mediaPlayer.isPlaying())
        {
            surfaceView1.requestFocus();
            mediaPlayer.start();
        }
    }

Solution

  • This is usually a result of failing to connect the MediaPlayer to the new Surface. When you switch activities the SurfaceView and its associated Surface are torn down, so when you come back you need to connect to the new Surface with MediaPlayer#setSurface().