Here is my code snippet:
exoPlayer.addListener(new Player.DefaultEventListener() {
@Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
super.onTracksChanged(trackGroups, trackSelections);
Log.i(TAG, "onTracksChanged: Tracks changed.");
}
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
super.onPlayerStateChanged(playWhenReady, playbackState);
Log.i(TAG, "onPlayerStateChanged: Player state changed: " + playbackState);
for (Callback callback : callbacks) {
callback.onPlayStateChanged(playbackState);
}
// 1: IDLE 2:BUFFERING 3:READY 4:ENDED
switch (playbackState) {
case Player.STATE_ENDED:
for (Callback callback : callbacks) {
callback.onCompletion();
}
break;
case Player.STATE_READY:
for (Callback callback : callbacks) {
callback.onPrepared();
}
break;
case Player.STATE_BUFFERING:
break;
case Player.STATE_IDLE:
break;
}
}});
onTrackChanged gets called but STATE_ENDED doesn't get called.
I set the repeat mode to repeat all, if it's releavent
if (exoPlayer.getRepeatMode() == Player.REPEAT_MODE_OFF) {
exoPlayer.setRepeatMode(Player.REPEAT_MODE_ALL);
}
STATE_ENDED means that the player has finished playing, which is not the case in your example because it's still playing something (due to the repeat mode).
If you're trying to detect when playback moves from one item in a playlist to the next, or when it restarts playback in the case that you're playing a single piece of media, then listen for EventListener.onPositionDiscontinuity with reason = Player. DISCONTINUITY_REASON_PERIOD_TRANSITION.
Most helpful comment
STATE_ENDEDmeans that the player has finished playing, which is not the case in your example because it's still playing something (due to the repeat mode).If you're trying to detect when playback moves from one item in a playlist to the next, or when it restarts playback in the case that you're playing a single piece of media, then listen for
EventListener.onPositionDiscontinuitywithreason = Player. DISCONTINUITY_REASON_PERIOD_TRANSITION.