ExoPlayer Restore State when Resumed

前端 未结 2 1276
时光说笑
时光说笑 2020-12-09 06:49

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

相关标签:
2条回答
  • 2020-12-09 07:13

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

     protected void onPause() {
        player.setPlayWhenReady(false);
        super.onPause();
    }
    
    protected void onResume() {
        player.setPlayWhenReady(true);
        super.onResume();
    }
    

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

    0 讨论(0)
  • 2020-12-09 07:20

    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:

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

    And onSaveInstanceState:

    currentState.putLong(SELECTED_POSITION, position);
    

    Last, I removed onDetach onDestroyView onStop.

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

    0 讨论(0)
提交回复
热议问题