I am new to android development and I am programming a game. My game has cutsceens that play before each level starts, cutsceens which are done through videoview. My problem is, that upon an application pause, the cutsceen starts from the beginning again when resumed.
@Override
public void onPause() {
super.onPause();
video.pause();
}
@Override
public void onResume()
{
super.onResume();
video.resume();
}
Our issue is that the video doesn't actually resume from where we paused it, but from the beginning.
You can use VideoView.getCurrentPosition()
to store the current position and VideoView.seekTo()
to go back to that point. That way you don't need the VideoView
to keep it's state.
Important is to use the correct listeners to only follow up with VideoView.start()
once the seek operation is completed.
@Override
public void onPause() {
// store position if it needs to survive configuration changes
int position = videoView.getCurrentPosition();
videoView.pause();
super.onPause();
}
@Override
public void onResume() {
super.onResume();
// use prepared listener to start seeking once video is loaded
videoView.setOnPreparedListener(mp -> {
mp.seekTo(position);
// use on seek complete listener to resume playback
mp.setOnSeekCompleteListener(smp -> smp.start());
});
}