How to pause ExoPlayer 2 playback and resume (PlayerControl was removed)
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()
.
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
@Overrideprotected void onPause() { super.onPause(); pausePlayer();}@Overrideprotected void onResume() { super.onResume(); startPlayer();}
playplayer.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
.