ExoPlayer 2 Playlist Listener
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; // ... } } });
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(); } });
Since the answer here did not work for me, I will add my working solution in Kotlin.And also there are lots of methods deprecated since then.
You can track the current track(video) by setting a mediaId when creating your playlist
Gradle:
implementation 'com.google.android.exoplayer:exoplayer:2.14.1'
Layout xml:
<com.google.android.exoplayer2.ui.PlayerView android:id="@+id/player_view" android:layout_width="match_parent" android:layout_height="100dp" app:show_buffering="when_playing"/>
Code:
private lateinit var exoPlayer: SimpleExoPlayerprivate lateinit var playerView: PlayerViewprivate lateinit var fileList: List<FileModel>private fun initPlayer() { playerView = findViewById(R.id.player_view) playerView.controllerShowTimeoutMs = 0 playerView.cameraDistance = 30F exoPlayer = SimpleExoPlayer.Builder(this).build() playerView.player = exoPlayer setPlaylist() exoPlayer.prepare() exoPlayer.seekTo(position, C.TIME_UNSET) // position = current song or video exoPlayer.playWhenReady = true addPlayerListeners()}private fun setPlaylist() { for ((pos, file) in fileList.withIndex()) { val mediaItem = MediaItem.Builder().setUri(file.url).setMediaId(pos.toString()).build() exoPlayer.addMediaItem(mediaItem) }}private fun addPlayerListeners() { exoPlayer.addListener(object : Player.Listener{ override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) { super.onMediaItemTransition(mediaItem, reason) val item = mediaItem?.mediaMetadata val bmp = item?.artworkData Log.i(TAG, "position = ${mediaItem?.mediaId}") val currentPos = mediaItem?.mediaId updateViews(currentPos, bmp) } })}private fun updateViews(currentPos: String?, bmp: ByteArray?) { position = currentPos?.toInt() ?: 0 val title = fileList[position].title textview_title.text = title if (bmp == null) { image_view.setImageResource(R.drawable.exo_ic_default_album_image) return } val bitmap = BitmapFactory.decodeByteArray(bmp, 0, bmp.size) image_view.setImageBitmap(bitmap)}