Reputation: 5401
I have been working on playing videos using VideoView in Android.
OnPause of fragment i am pausing the video and storing the seektime
@Override
public void onPause() {
super.onPause();
if (videoview != null && videoview.isPlaying()) {
videoview.pause();
seekTime = videoview.getCurrentPosition();
}
}
OnResume i am trying to resume the video using -
@Override
public void onResume() {
super.onResume();
videoview.seekTo(seekTime);
//vvOnboardingVideos.resume();// this is not working for now - need to investigate
videoview.start();
}
The video plays from the beginning.
Is there anything i am doing wrong. Please suggest.
Upvotes: 3
Views: 3108
Reputation: 3496
Try to reverse order of operations :
@Override
public void onResume() {
super.onResume();
videoview.start();
videoview.seekTo(seekTime);
}
Upvotes: 1
Reputation: 23881
try this:
save the video position when onpause()
called
// This gets called before onPause
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
stopPosition = videoView.getCurrentPosition();
videoView.pause();
outState.putInt("video_position", stopPosition);
}
Then in your onCreate()
load the stopPosition
from the bundle
@Override
protected void onCreate(Bundle args) {
super.onCreate(args);
if( args != null ) {
stopPosition = args.getInt("video_position");
}
}
Resume the video using:
@Override
public void onResume() {
super.onResume();
Log.d("Tag", "onResume");
videoView.seekTo(stopPosition);
videoView.start(); //Or use videoView.resume() if it doesn't work.
}
Upvotes: 0