Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect pause/resume in ExoPlayer

I searched two days for this question in github but i can't find true answer . I want example for detecting pause / resume in ExoPlayer > 2.x . Any one can give me an example ? I checked onPlayerStateChanged and problem not solved .

onPlayerStateChanged   :   STATE_BUFFERING  onPlayerStateChanged   :   STATE_READY  

I just got this log from onPlayerStateChanged and this is not called in all times !

like image 260
MrNadimi Avatar asked Dec 09 '17 18:12

MrNadimi


2 Answers

EDIT---

Please refer to the Player.isPlaying() method which provides this as an API.

"Rather than having to check these properties individually, Player.isPlaying can be called."

https://exoplayer.dev/listening-to-player-events.html#playback-state-changes

--- EDIT END

You need to check playWhenReady with a Player.EventListener. The Playback states of ExoPlayer are independent from the player being paused or not:

player.addListener(new Player.DefaultEventListener() {   @Override   public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {     if (playWhenReady && playbackState == Player.STATE_READY) {       // media actually playing     } else if (playWhenReady) {       // might be idle (plays after prepare()),        // buffering (plays when data available)        // or ended (plays when seek away from end)     } else {       // player paused in any state     }   } }); 

To play/pause the player ExoPlayer provides

player.setPlayWhenReady(boolean) 

The sequence of playback states with ExoPlayer with a media file which never stalls to rebuffer is once in each of the four states and does not express play/paused:

Player.STATE_IDLE; Player.STATE_BUFFERING; Player.STATE_READY; Player.STATE_ENDED; 

Each time the player needs to buffer it goes:

Player.STATE_READY; Player.STATE_BUFFERING; Player.STATE_READY; 

Setting playWhenReady does not affect the state.

All together your media is actually playing when

playWhenReady && playbackState == Player.STATE_READY 

It plays when ready. :)

like image 52
marcbaechinger Avatar answered Oct 06 '22 10:10

marcbaechinger


You can use this function:

public boolean isPlaying() {     return exoPlayer.getPlaybackState() == Player.STATE_READY && exoPlayer.getPlayWhenReady(); } 
like image 40
Payam Roozbahani Fard Avatar answered Oct 06 '22 09:10

Payam Roozbahani Fard