Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 934
heero Avatar asked Aug 14 '12 21:08

heero


Video Answer


2 Answers

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");     } 
like image 79
iTurki Avatar answered Sep 21 '22 23:09

iTurki


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!

like image 32
Abhishek V Avatar answered Sep 22 '22 23:09

Abhishek V