ExoPlayer STATE_ENDED not called after song is finished

Created on 25 Aug 2018  路  1Comment  路  Source: google/ExoPlayer

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);
        }
question

Most helpful comment

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.

>All comments

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.

Was this page helpful?
0 / 5 - 0 ratings