Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the index of the current track being played using ExoPlayer

I am working on an Android project that involves the use of Google's ExoPlayer. I have a list of video sources which I build a playlist from using the following code:

for (int i = 0; i < vidList.length(); i++) {
    MediaSource source = new ExtractorMediaSource(Uri.parse(vidList.getJSONObject(i).getString("url")),
                                buildDataSourceFactory(bandwidthMeter), extractorsFactory, mainHandler, HomeFragment.this);
                        mediaSources.add(source);
                        captions.add(vidList.getJSONObject(i).getString("caption"));
                    }

    mediaSource = new ConcatenatingMediaSource(mediaSources.toArray(new MediaSource[mediaSources.size()]));

I then call

exoplayer.prepare(mediasource, false, false)

and the videos play in succession fine. I would like to display the caption of the currently playing video in a textView and so I have a separate list that holds the "caption" values for each video.

From scouring through the code I see that I can get the currently playing video in the playlist like this;

exoPlayer.getCurrentPeriodIndex()

Which seems to work and returns the index except for one problem. It returns the value of 0 twice as playback starts. That is video at index 0 returns period 0 as well as video at index 1. This only occurs at indexes 0 and 1 and thereafter everything else looks fine except that the getCurrentPeriodIndex() will return theAccurateIndex - 1.

I see this also happening in the demo Exoplayer application. Is there a better way to determine what track is currently playing in the playlist?

Thanks.

like image 834
Jome Avatar asked Sep 06 '25 20:09

Jome


2 Answers

To find the currently playing track, you need to reference currentWindowIndex exoPlayer field. Looks like this in Java...

exoPlayer.getCurrentWindowIndex()

I'm not sure what getCurrentPeriodIndex() does, and the docs don't elaborate, and I don't like speculating.

like image 105
wooldridgetm Avatar answered Sep 10 '25 13:09

wooldridgetm


exoPlayer.getCurrentWindowIndex() is Deprecated.

Use exoPlayer.getCurrentMediaItemIndex() instead.

like image 23
Lins Louis Avatar answered Sep 10 '25 14:09

Lins Louis