Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExoPlayer 2 Playlist Listener

Tags:

I'm using the new features from ExoPlayer 2.x to play a list of audio files like this:

List<MediaSource> playlist = new ArrayList<>();  ...  ConcatenatingMediaSource concatenatedSource = new ConcatenatingMediaSource(             playlist.toArray(new MediaSource[playlist.size()]));  mExoPlayer.prepare(concatenatedSource); mExoPlayer.setPlayWhenReady(true); 

This is working fine, but in order to update my UI accordingly, I need to know which track is currently playing and the progress of this track. Is there any listener from ExoPlayer?

Thanks!

like image 268
timoschloesser Avatar asked Oct 27 '16 12:10

timoschloesser


People also ask

How do you play the next ExoPlayer video?

1 Answer. Show activity on this post. 3] Just check by clicking next button from the media controller if that works then you are done, now the videos will be played automatically once finished the current one.

How do you add a listener to ExoPlayer?

You can do this: playerExo. addListener(new ExoPlayer. Listener() { @Override public void onPlayerStateChanged(boolean playWhenReady, int playbackState) { switch(playbackState) { case ExoPlayer.

How do I know if ExoPlayer is playing?

You can use exoplayer. getPlayWhenReady() to check whether the player is currently in a pause state or playing state.


2 Answers

So I am in a similar scenario and need to know when the next video in the playlist starts. I found that the ExoPlayer.EventListener has a method called onPositionDiscontinuity() that gets called every time the video changes or "seeks" to the next in the playlist.

I haven't played around with this method extensively, but from what I can see so far, this is the method that you should be concerned about. There are no parameters that get passed when the method is fired, so you'll have to keep some kind of counter or queue to keep track of whats being played at any given moment.

Hopefully this helps!

Edit: change in index returned by Exoplayer.getCurrentWindowIndex() is the recommended way to detect item change in a playlist MediaSource.

int lastWindowIndex = 0; // global var in your class encapsulating exoplayer obj (Activity, etc.)  exoPlayer.addListener(new ExoPlayer.EventListener() {         @Override         public void onLoadingChanged(boolean isLoading) {         }          @Override         public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {         }          @Override         public void onTimelineChanged(Timeline timeline, Object manifest) {         }          @Override         public void onPlayerError(ExoPlaybackException error) {         }          @Override         public void onPositionDiscontinuity() {             //THIS METHOD GETS CALLED FOR EVERY NEW SOURCE THAT IS PLAYED             int latestWindowIndex = exoPlayer.getCurrentWindowIndex();             if (latestWindowIndex != lastWindowIndex) {                 // item selected in playlist has changed, handle here                 lastWindowIndex = latestWindowIndex;                 // ...             }         }     }); 
like image 79
raisedandglazed Avatar answered Sep 22 '22 17:09

raisedandglazed


You can implement the following event and update your ui according to the player's state.

 mExoPlayer.addListener(new ExoPlayer.Listener() {             @Override             public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {                 if (playbackState == PlaybackStateCompat.STATE_PLAYING) {                     //do something                                  }             }              @Override             public void onPlayWhenReadyCommitted() {              }              @Override             public void onPlayerError(ExoPlaybackException error) {                 mExoPlayer.stop();              }         }); 
like image 34
DoronK Avatar answered Sep 23 '22 17:09

DoronK