Skip to content Skip to sidebar Skip to footer

Exoplayer Restore State When Resumed

I have implemented the Player and now there is a problem. When the video is playing and if the app is closed and resumed, the video screen freezes. I even saw the ExoPlayer Demo Ac

Solution 1:

You can store the player position on pause:

position = player.getCurrentPosition(); //then, save it on the bundle.

And then when you restore it, if it is there, you can do:

if (position != C.TIME_UNSET) player.seekTo(position);

before prepare() in the initializePlayer() method.

Ok, I cloned the project, and made it work. What I changed basically is:

I added what I said before, and then:

position = C.TIME_UNSET;
if (savedInstanceState != null) {
    //...your code...
    position = savedInstanceState.getLong(SELECTED_POSITION, C.TIME_UNSET);
}

I made the videoUri global

videoUri = Uri.parse(steps.get(selectedIndex).getVideoURL());

Added onResume:

@Override
public void onResume() {
    super.onResume();
    if (videoUri != null)
        initializePlayer(videoUri);
}

Updated onPause:

@OverridepublicvoidonPause() {
    super.onPause();
    if (player != null) {
        position = player.getCurrentPosition();
        player.stop();
        player.release();
        player = null;
    }
}

And onSaveInstanceState:

currentState.putLong(SELECTED_POSITION, position);

Last, I removed onDetachonDestroyViewonStop.

Obviously this is "just to make it work", you will have to work more on it.

Solution 2:

I know this is an old thread but, here is my fix

protectedvoidonPause() {
    player.setPlayWhenReady(false);
    super.onPause();
}

protectedvoidonResume() {
    player.setPlayWhenReady(true);
    super.onResume();
}

this will pause the video on activity pause and resume on activity resume.

Post a Comment for "Exoplayer Restore State When Resumed"