Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 999
Daniel Gomez Rico Avatar asked Nov 11 '16 20:11

Daniel Gomez Rico


People also ask

How to play pause ExoPlayer in Android?

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

How do I know if ExoPlayer is playing?

You can use exoplayer. getPlayWhenReady() to check whether the player is currently in a pause state or playing state.


2 Answers

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

like image 98
Blackbelt Avatar answered Sep 25 '22 11:09

Blackbelt


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(); } 
like image 37
Ittai Oren Avatar answered Sep 25 '22 11:09

Ittai Oren