How to pause ExoPlayer 2 playback and resume (PlayerControl was removed)

后端 未结 3 595
自闭症患者
自闭症患者 2020-12-25 11:33

In ExoPlayer < 2.x there was a class PlayerControl with pause() and resume() functions but it was removed. I can\'t fi

相关标签:
3条回答
  • 2020-12-25 12:18

    This is my way. Create two methods and call them when needed.

    private void pausePlayer(){
        player.setPlayWhenReady(false);
        player.getPlaybackState();
    }
    private void startPlayer(){
        player.setPlayWhenReady(true);
        player.getPlaybackState();
    }
    

    call them here

     @Override
    protected void onPause() {
        super.onPause();
       pausePlayer();
    
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        startPlayer();
    }
    
    0 讨论(0)
  • 2020-12-25 12:27

    You can use void setPlayWhenReady(boolean playWhenReady).
    If Exo is ready, passing false will pause the player. Passing true will resume it. You can check the player's state using getPlaybackState().

    0 讨论(0)
  • 2020-12-25 12:27

    play player.setPlayWhenReady(true);

    pause

    player.setPlayWhenReady(false);

    And you can check play state like this:

    private boolean isPlaying() {
    return player != null
        && player.getPlaybackState() != Player.STATE_ENDED
        && player.getPlaybackState() != Player.STATE_IDLE
        && player.getPlayWhenReady();
    }
    

    These codes are from PlayerControlView.

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