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

巧了我就是萌 提交于 2019-11-30 04:46:28

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

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();
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!