22

I have a VideoView and it is pause when I start an Email intent. When the email intent is done, I want the videoView to continue playing, however, it restarts from the beginning.

@Override
public void onPause() {
    Log.d(TAG, "onPause called");
    super.onPause();
    videoView.pause();
}
@Override
public void onResume() {
    super.onResume();
    Log.d(TAG, "onResume called");
    videoView.start();//resume() doesnt work
}

How can I get the videoView to resume from where it left off.

heero
  • 1,941
  • 5
  • 23
  • 33

2 Answers2

34

What about this:

@Override
public void onResume() {
    super.onResume();
    Log.d(TAG, "onResume called");
    videoView.seekTo(stopPosition);
    videoView.start(); //Or use resume() if it doesn't work. I'm not sure
}

// This gets called before onPause so pause video here.
@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    stopPosition = videoView.getCurrentPosition(); 
    videoView.pause();
    outState.putInt("position", stopPosition);
}   

Then in your onCreate() call the stopPosition from the bundle and set it globally

@Override
protected void onCreate(Bundle args) {
    super.onCreate(args);
    if( args != null ) {
        stopPosition = args.getInt("position");
    }
JPM
  • 9,077
  • 13
  • 78
  • 137
iTurki
  • 16,292
  • 20
  • 87
  • 132
  • 1
    you can store your 'stopPosition' in the outState during onSaveInstanceState(Bundle outState), than you can pick it up in your activities using onCreate(Bundle savedInstanceState). – Entreco Dec 28 '12 at 15:00
  • Please look into this : https://stackoverflow.com/questions/59138353/media-player-java-lang-illegalstateexception-when-resuming-activity-from-pause-s – user2028 Dec 02 '19 at 13:10
22

Using seekTo gives a slight flickering efeect while resuming the video. Better approach would be using pause() and start() methods of MediaPlayer class instead of using methods from VideoView. The start() method of VideoView restarts the video from the beginning, but start() method of MediaPlayer resumes the video if the pause() method had been previously called.

Here is what official android doc says

public void start ()

Added in API level 1 Starts or resumes playback. If playback had previously been paused, playback will continue from where it was paused. If playback had been stopped, or never started before, playback will start at the beginning.

You can get the MediaPlayer associated with a VideoView in OnPreparedListener of VideoView.

public class MainActivity extends Activity {

    private MediaPlayer mMediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        VideoView videoView = (VideoView) findViewById(R.id.videoView1);
        videoView.setVideoPath(path);
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mp) {
                mMediaPlayer = mp;

            }
        });
    }

}

Then subsequent pause() and resume() methods can be called on MediaPlayer object itself.

//To pause the video
 mMediaPlayer.pause();

//To resume the video
mMediaPlayer.start();

Very simple yet efficient solution. Hope it helps!

Abhishek V
  • 12,488
  • 6
  • 51
  • 63
  • Excellent answer! But... I am wondering why the suspend() and resume() methods?? If pause/start do the trick. – Nicolas Jafelle Jun 05 '14 at 14:42
  • 4
    @NicolasJafelle VideoView `resume` method doesn't seem to be working. It starts the video from the beginning. May be some bug with the `VideoView`. And `suspend()` is to release the video from memory. Internally it executes `MediaPlayer` `relese()`. – Abhishek V Jun 05 '14 at 15:06
  • 6
    In my app it gives illegalStateException for the call mMediaPlayer.start(); in the on resume – beginner Aug 10 '16 at 12:48
  • "The start() method of VideoView restarts the video from the beginning, but start() method of MediaPlayer resumes the video if the pause() method had been previously called." The start method of VideoView simply calls the start method of MediaPlayer, so they are one and the same. VideoView start method code: `public void start() { if (isInPlaybackState()) { mMediaPlayer.start(); mCurrentState = STATE_PLAYING; } mTargetState = STATE_PLAYING; }` – idunno Jul 31 '17 at 09:41
  • 1
    Thanks for the answer. I am banging my head on the wall for hours to fix the video flicker. This answer saved me further headache. – JaydeepW May 25 '20 at 08:19