heero
heero

Reputation: 1971

Resume playing VideoView from onResume

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.

Upvotes: 22

Views: 22679

Answers (2)

iTurki
iTurki

Reputation: 16398

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");
    }

Upvotes: 34

Abhishek V
Abhishek V

Reputation: 12526

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!

Upvotes: 22

Related Questions