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

37,773

Solution 1

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

Solution 2

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

Solution 3

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.

Share:
37,773
Daniel Gomez Rico
Author by

Daniel Gomez Rico

Me

Updated on July 16, 2022

Comments

  • Daniel Gomez Rico
    Daniel Gomez Rico almost 2 years

    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?

  • Akash Dubey
    Akash Dubey almost 7 years
    Kindly Answer with code so one can understand it correctly
  • Daniel Gomez Rico
    Daniel Gomez Rico almost 7 years
    Why do you call player.getPlaybackState(); after the "play when ready" changed?
  • Saeed.re
    Saeed.re over 6 years
    To pause and play with the same button You can use : player.setPlayWhenReady(!player.getPlayWhenReady()); .
  • Sagar
    Sagar about 6 years
    Exoplayer take 1sec/2sec on resume @Override protected void onResume() { super.onResume(); startPlayer(); } how can i solve this audio plays but videos start late
  • android developer
    android developer almost 6 years
    @Caipivara It's not needed. Maybe he wanted to show it's possible to use it.
  • Vlad
    Vlad about 5 years
    Why not simply player.getPlaybackState()== Player.STATE_READY