Detect Pause/resume In Exoplayer
Solution 1:
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(newPlayer.DefaultEventListener() {
@OverridepublicvoidonPlayerStateChanged(boolean playWhenReady, int playbackState) {
if (playWhenReady && playbackState == Player.STATE_READY) {
// media actually playing
} elseif (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. :)
Solution 2:
You can use this function:
publicbooleanisPlaying() {
return exoPlayer.getPlaybackState() == Player.STATE_READY && exoPlayer.getPlayWhenReady();
}
Solution 3:
It must be that since the other answers were posted, a new method has been provided in Player.EventListener. [EDIT: Now it is Player.Listener, as Player.EventListener has been deprecated]. This works well for me:
overridefunonIsPlayingChanged(isPlaying: Boolean) {
// your code here
}
If isPlaying is false, it is paused, otherwise playing.
Solution 4:
I had the same requirement to detect the click event of exoplayer play/pause button. Above answers were mainly talking about the state not about the button click event.
This is what I did to detect the Play/Pause button click, works perfect.
Step 1: Create custom control dispatcher class and override the method dispatchSetPlayWhenReady
classPlayerControlDispatcher : DefaultControlDispatcher() {
overridefundispatchSetPlayWhenReady(player: Player?, playWhenReady: Boolean): Boolean {
if(playWhenReady) {
// Play button clicked
} else {
// Paused button clicked
}
returnsuper.dispatchSetPlayWhenReady(player, playWhenReady)
}
}
Step 2: Set the custom control dispatcher class PlayerControlDispatcher
into the the player view.
playerView.setControlDispatcher(PlayerControlDispatcher())
Where playerView is an instance of com.google.android.exoplayer2.ui.PlayerView
which we declare in our layout file.
Solution 5:
Kotlin 2020 solution approach UPDATE
Events such as changes in state and playback errors are reported to registered Player.EventListener
instances.
Player.EventListener
has empty default methods, so you only need to implement the methods you’re interested in.
First your class, say your activity, has te conform to the Player.EventListener
interface.
Then you override the onIsPlayingChanged
method, on the class. Outside onCreate
method...
Add the listener to your player instance:
// Adding player listener for tracking events
player?.addListener(this)
You can check if the player is playing (i.e. the position is advancing) with Player.isPlaying:
//Listening to player eventsoverridefunonIsPlayingChanged(isPlaying: Boolean){
if (isPlaying) {
// Active playback.
} else {
// Not playing because playback is paused, ended, suppressed, or the player// is buffering, stopped or failed. Check player.getPlaybackState,// player.getPlayWhenReady, player.getPlaybackError and// player.getPlaybackSuppressionReason for details.
}
}
That's it. Very simple.
Post a Comment for "Detect Pause/resume In Exoplayer"