How to implement oncompletionlistener to detect the end of a media file in Exoplayer

I am trying to play a playlist video one by one. I use Android Exoplayer to play my files, but there are no listeners in the media player who could listen to the end of the media file. Is there a way to listen to the end of a media file using exoplayer.

+18
source share
9 answers

Exoplayer offers a preliminary implementation that is not in the media player.

in the extended Exoplayer example for Android, in FullPlayerActivity.java they implemented onStateChanged , which offers STATE_ENDED

You can download an example from the right section of this page under the term RELATED SAMPLES

+27
source

I know this is old, but just to expand on the accepted answer:

exoPlayer.addListener(this); ..... @Override public void onPlayerStateChanged(boolean playWhenReady, int state) { if (state == ExoPlayer.STATE_ENDED){ //player back ended } } 
+31
source

Google has deprecated ExoPlayer.STATE_ENDED and replaced it with Player.STATE_ENDED .

Since this post has been around for a long time, I will write a listener version on Kotlin below.

 this.simpleExoPlayer?.addListener(object : Player.DefaultEventListener() { override fun onPlayerStateChanged(playWhenReady: Boolean,playbackState: Int) { when (playbackState) { Player.STATE_IDLE -> {} Player.STATE_BUFFERING -> {} Player.STATE_READY -> {} Player.STATE_ENDED -> {} } } }) 
+9
source

You can do it:

 playerExo.addListener(new ExoPlayer.Listener() { @Override public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { switch(playbackState) { case ExoPlayer.STATE_BUFFERING: break; case ExoPlayer.STATE_ENDED: //do what you want break; case ExoPlayer.STATE_IDLE: break; case ExoPlayer.STATE_PREPARING: break; case ExoPlayer.STATE_READY: break; default: break; } } @Override public void onPlayWhenReadyCommitted() { } @Override public void onPlayerError(ExoPlaybackException error) { } }); playerExo.seekTo(0); playerExo.setPlayWhenReady(true); 

But the answer @ Murtaza Khurshid Hussein is right! Have a nice day of code! Let me know if you need anything else!

+8
source

You must implement the Player.EventLister interface and add it to exoPlayer.

Just write your code using the onPlayerStateChanged method. See the code.

 exoPlayer.addListener( new Player.EventListener() { @Override public void onTimelineChanged(Timeline timeline, @Nullable Object manifest, int reason) { } @Override public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) { } @Override public void onLoadingChanged(boolean isLoading) { } @Override public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { switch(playbackState) { case Player.STATE_BUFFERING: break; case Player.STATE_ENDED: //Here you do what you want break; case Player.STATE_IDLE: break; case Player.STATE_READY: break; default: break; } } @Override public void onRepeatModeChanged(int repeatMode) { } @Override public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) { } @Override public void onPlayerError(ExoPlaybackException error) { } @Override public void onPositionDiscontinuity(int reason) { } @Override public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) { } @Override public void onSeekProcessed() { } }); 
+6
source

ExoPlayer.STATE_ENDED is depreciating. Use Player.STATE_ENDED instead. Example:

 @Override public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { if(playbackState == Player.STATE_ENDED){ // your brain put here } } 
+2
source

I used a playlist with exoplayer and wanted to stop the multimedia after it was finished.

For me, Player.STATE_ENDED was called when the playlist ended for me inside

Therefore, in order to fulfill my use case, I first installed this in exoplayer

  simpleExoPlayer.setRepeatMode(Player.REPEAT_MODE_ONE); 

So instead of using EventListener, I used AnalyticsListener , which allows onPositionDiscontinuity to be overridden with the eventTime parameter and just add simple code to find out if the media restarted.

  @Override public void onPositionDiscontinuity(EventTime eventTime, int reason) { if (eventTime.currentPlaybackPositionMs / 1000 < 1) { exoPlayer.setPlayWhenReady(false); tvReplay.setVisibility(View.VISIBLE); } } 

Hope this helps you

0
source

Google does not support ExoPlayer.STATE_ENDED. Use Player.STATE_ENDED instead. See the code below.

 player.addListener(new Player.EventListener() { @Override public void onTimelineChanged(Timeline timeline, Object manifest, int reason) { } @Override public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) { } @Override public void onLoadingChanged(boolean isLoading) { } @Override public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { if (playWhenReady && playbackState == Player.STATE_READY) { // Active playback. } else if (playbackState == Player.STATE_ENDED) { //The player finished playing all media //Add your code here } else if (playWhenReady) { // Not playing because playback ended, the player is buffering, stopped or // failed. Check playbackState and player.getPlaybackError for details. } else { // Paused by app. } } @Override public void onRepeatModeChanged(int repeatMode) { } @Override public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) { } @Override public void onPlayerError(ExoPlaybackException error) { } @Override public void onPositionDiscontinuity(int reason) { //THIS METHOD GETS CALLED FOR EVERY NEW SOURCE THAT IS PLAYED // int latestWindowIndex = player.getCurrentWindowIndex(); } @Override public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) { } @Override public void onSeekProcessed() { } }); 

You can check the ExoPlayer developer page for more information.

0
source

For a non-concatenated MediaSource (representing any particular media fragment that you want to play), you will receive STATE_ENDED after the media fragment has finished playing.

For ConcatenatingMediaSource, this happens when all concatenation is over (i.e. you played to the end of the last element in concatenation). So STATE_ENDED happens when the entire MediaSource has finished playing.

For ConcatenatingMediaSource, the best callback for determining the end of the current media and the start of the next media playback is "onPositionDiscontinuity" You should use onPositionDiscontinuity () to find out when transitions occur. Note that onPositionDiscontinuity is also called for some other cases, but you can call getCurrentWindowIndex () and compare it with the window you are in to determine if the transition has occurred. You can do something like below:

 public void onPositionDiscontinuity() { int newIndex = player.getCurrentWindowIndex(); if (newIndex != currentIndex) { // The index has changed ==> last media has ended and next media playback is started. currentIndex = newIndex; } } 

Note: onPositionDiscontinuity () is not called for the 1st item in the playlist unless we explicitly call player.seekTo (position, 0). Therefore, to process everything that you do to play all the multimedia in the playlist, you must process separately for the 1st item in the playlist.

0
source

Source: https://habr.com/ru/post/1208556/


All Articles