android: videoview history/bookmark

2019-08-13 18:10发布

问题:

I have a videoplayer application. Suppose the user has played a video and after playing half of the video, the user pressed the back button or exists the application. I want that position to be remembered so that the next time, the user plays the same video, the video starts from exactly the same position from where it was left.

Any ideas in this regard? Thanks much

回答1:

First, you will need to store this data in some database, but it's quite simple.

Now, you should add a check in the onPause() or onDestroy() method of your activity, which gets the position from the videoview:

public void onPause() {
// ....
   int position = myVideoView.getCurrentPosition();
// store the position and file name (you should have it from before)
}

When you play a video, set the current position as stored before:

private void dummyPlayVideo(String fileName) {
// ....
    int position = getSavedPositionOfVideo(fileName); // your method
    myVideoView.seekTo(position);
}