Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Exoplayer ConcatenatingMediaSource detect end of first source

I am trying add an intro video to my actual video. I am planning to achieve this by using ConcatenatingMediaSource. Below is the source code

DataSource.Factory dataSourceFactory = new CacheDataSourceFactory(VideoCache.getInstance(this), new DefaultDataSourceFactory(this, "test"));
    MediaSource firstSource = new ExtractorMediaSource(Uri.parse("path1.mp4"),
            dataSourceFactory, new DefaultExtractorsFactory(), null, null);
    MediaSource secondSource = new ExtractorMediaSource(Uri.parse("path2.mp4"),
            mediaDataSourceFactory, extractorsFactory, null, null);
// Plays the first video, then the second video.

ConcatenatingMediaSource concatenatedSource =
            new ConcatenatingMediaSource(firstSource, secondSource);
    player.prepare(concatenatedSource);

I need to know when the intro video stops playing so I can make some UI changes as well as start showing the controller layout for the video. One way I have tried, is to set a CountDownTimer with a hardcoded value which does the necessary changes once onFinish is called. I was wondering if there are any listeners which will help me get a callback for when a source ends. Is onTracksChanged a proper callback to consider?

like image 695
Rohan M Avatar asked Dec 20 '18 10:12

Rohan M


1 Answers

In your case, both onTrackChanged() and onPositionDiscontinuity() callbacks will be called when the second video is start to play.

onPositionDiscontinuity() will also be invoked during a seek operation. You can get the newly changed window index by calling player.getCurrentWindowIndex() inside it. On the other hand, calling player.getCurrentWindowIndex() inside onTrackChanged() will not be guaranteed to get the right index.

UPDATE:

there is a section in the documentation explained how to detect playback transitions.

There are three types of events that may be called when the current playback item changes:

EventListener.onPositionDiscontinuity with reason = Player.DISCONTINUITY_REASON_PERIOD_TRANSITION. This happens when playback automatically transitions from one item to the next.

EventListener.onPositionDiscontinuity with reason = Player.DISCONTINUITY_REASON_SEEK. This happens when the current playback item changes as part of a seek operation, for example when calling Player.next.

EventListener.onTimelineChanged with reason = Player.TIMELINE_CHANGE_REASON_DYNAMIC. This happens when the playlist changes, e.g. if items are added, moved, or removed.

In all cases, when your application code receives the event, you can query the player to determine which item in the playlist is now being played. This can be done using methods such as Player.getCurrentWindowIndex and Player.getCurrentTag. If you only want to detect playlist item changes, then it’s necessary to compare against the last known window index or tag, because the mentioned events may be triggered for other reasons.

like image 167
Wenqing MA Avatar answered Nov 12 '22 22:11

Wenqing MA