Reputation: 809
I am not able to save the state of each video respectively. Currently, when I press back from a playing video, I save the last time of that video and when I click on any other video it resumes to the last saved position. But I want each position to be saved independently.
What I am doing:
@Override
public void onBackPressed() {
super.onBackPressed();
videoView.pause();
mPlayVideoWhenForegrounded = videoView.getPlayer().getPlayWhenReady();
// Store off the last position our player was in before we paused it.
mLastPosition = videoView.getPlayer().getCurrentPosition();
// Pause the player
videoView.getPlayer().setPlayWhenReady(false);
//Here I am saving the last position in the database
sqLiteDatabaseHandler.add(new Contact(mLastPosition,""));
}
@Override
protected void onStart() {
super.onStart();
if (Build.VERSION.SDK_INT > 23) {
videoView.resume();
}
//getting db values
results = (ArrayList) sqLiteDatabaseHandler.getAll();
if(results.size()>0){
for (int i = 0; i < results.size(); i++) {
//value from databse of mLastPosition saved in onBackPressed()
seekto=results.get(i).getmLastPosition();
}
//seekto returns int value as 5604 or 7400
if(seekto>1000){
videoView.getPlayer().seekTo(seekto);
}
}
}
What is the issue I am facing:
With the above logic, I am able to pause and resume the video from where left off. But it resumes each video from the mLastPosition saved of any video. I want to save each video position differently. Please give me a logic on how can I store each video updated mLastPosition to resume it from the same. Basically, I want each should resume from a different time where it was paused.
Upvotes: 2
Views: 993
Reputation: 823
You need to store the current window, the current position, and possibly the state of playWhenReady each in a variable, so you can resume the player later. We can do something like the below:
Store the player position in onPause:
public void onPause() {
super.onPause();
player.setPlayWhenReady(false);
if (player != null) {
mLastPosition = player.getCurrentPosition();
…
}
}
Save the last playback position into the bundle:
public void onSaveInstanceState(Bundle currentState) {
super.onSaveInstanceState(currentState);
currentState.putLong(SELECTED_POSITION, mLastPosition);
currentState.putParcelable(KEY_TRACK_SELECTOR_PARAMETERS, trackSelectorParameters);
currentState.putInt(KEY_WINDOW, startWindow);
currentState.putLong(KEY_POSITION, startPosition);
currentState.putBoolean(KEY_AUTO_PLAY, startAutoPlay);
}
Initiate the player again in onResume and set seek to the last position of the player:
public void onResume() {
player.setPlayWhenReady(true);
super.onResume();
//initiateExoPlayerPlayer();
if(mLastPosition!=0 && player!=null){
player.seekTo(mLastPosition);
}
}
Have a look here.
Upvotes: 1