40

In ExoPlayer < 2.x there was a class PlayerControl with pause() and resume() functions but it was removed. I can't find a way to do this on ExoPlayer 2.

How can I pause and resume a playback?

Vadim Kotov
  • 7,103
  • 8
  • 44
  • 57
Daniel Gomez Rico
  • 12,814
  • 14
  • 81
  • 140

3 Answers3

83

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().

Vadim Kotov
  • 7,103
  • 8
  • 44
  • 57
Blackbelt
  • 148,780
  • 26
  • 271
  • 285
41

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();
}
Ittai Oren
  • 512
  • 5
  • 10
2

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.

linkaipeng
  • 305
  • 3
  • 7