Search in sources :

Example 1 with CompositeMediaSource

use of androidx.media3.exoplayer.source.CompositeMediaSource in project media by androidx.

the class ExoPlayerTest method delegatingMediaSourceApproach.

@Test
public void delegatingMediaSourceApproach() throws Exception {
    Timeline fakeTimeline = new FakeTimeline(new TimelineWindowDefinition(/* isSeekable= */
    true, /* isDynamic= */
    false, /* durationUs= */
    10_000_000));
    final ConcatenatingMediaSource underlyingSource = new ConcatenatingMediaSource();
    CompositeMediaSource<Void> delegatingMediaSource = new CompositeMediaSource<Void>() {

        @Override
        public void prepareSourceInternal(@Nullable TransferListener mediaTransferListener) {
            super.prepareSourceInternal(mediaTransferListener);
            underlyingSource.addMediaSource(new FakeMediaSource(fakeTimeline, ExoPlayerTestRunner.VIDEO_FORMAT));
            underlyingSource.addMediaSource(new FakeMediaSource(fakeTimeline, ExoPlayerTestRunner.VIDEO_FORMAT));
            prepareChildSource(null, underlyingSource);
        }

        @Override
        public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator, long startPositionUs) {
            return underlyingSource.createPeriod(id, allocator, startPositionUs);
        }

        @Override
        public void releasePeriod(MediaPeriod mediaPeriod) {
            underlyingSource.releasePeriod(mediaPeriod);
        }

        @Override
        protected void onChildSourceInfoRefreshed(Void id, MediaSource mediaSource, Timeline timeline) {
            refreshSourceInfo(timeline);
        }

        @Override
        public boolean isSingleWindow() {
            return false;
        }

        @Override
        public MediaItem getMediaItem() {
            return underlyingSource.getMediaItem();
        }

        @Override
        @Nullable
        public Timeline getInitialTimeline() {
            return Timeline.EMPTY;
        }
    };
    int[] currentMediaItemIndices = new int[1];
    long[] currentPlaybackPositions = new long[1];
    long[] windowCounts = new long[1];
    int seekToMediaItemIndex = 1;
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).seek(/* mediaItemIndex= */
    1, /* positionMs= */
    5000).waitForTimelineChanged(/* expectedTimeline= */
    null, Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            currentMediaItemIndices[0] = player.getCurrentMediaItemIndex();
            currentPlaybackPositions[0] = player.getCurrentPosition();
            windowCounts[0] = player.getCurrentTimeline().getWindowCount();
        }
    }).build();
    ExoPlayerTestRunner exoPlayerTestRunner = new ExoPlayerTestRunner.Builder(context).setMediaSources(delegatingMediaSource).setActionSchedule(actionSchedule).build().start().blockUntilActionScheduleFinished(TIMEOUT_MS).blockUntilEnded(TIMEOUT_MS);
    exoPlayerTestRunner.assertTimelineChangeReasonsEqual(Player.TIMELINE_CHANGE_REASON_SOURCE_UPDATE);
    assertArrayEquals(new long[] { 2 }, windowCounts);
    assertArrayEquals(new int[] { seekToMediaItemIndex }, currentMediaItemIndices);
    assertArrayEquals(new long[] { 5_000 }, currentPlaybackPositions);
}
Also used : TransferListener(androidx.media3.datasource.TransferListener) Allocator(androidx.media3.exoplayer.upstream.Allocator) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) ActionSchedule(androidx.media3.test.utils.ActionSchedule) PlayerRunnable(androidx.media3.test.utils.ActionSchedule.PlayerRunnable) TestExoPlayerBuilder(androidx.media3.test.utils.TestExoPlayerBuilder) CompositeMediaSource(androidx.media3.exoplayer.source.CompositeMediaSource) Timeline(androidx.media3.common.Timeline) NoUidTimeline(androidx.media3.test.utils.NoUidTimeline) FakeTimeline(androidx.media3.test.utils.FakeTimeline) SinglePeriodTimeline(androidx.media3.exoplayer.source.SinglePeriodTimeline) CompositeMediaSource(androidx.media3.exoplayer.source.CompositeMediaSource) ClippingMediaSource(androidx.media3.exoplayer.source.ClippingMediaSource) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) MaskingMediaSource(androidx.media3.exoplayer.source.MaskingMediaSource) ServerSideAdInsertionMediaSource(androidx.media3.exoplayer.source.ads.ServerSideAdInsertionMediaSource) FakeAdaptiveMediaSource(androidx.media3.test.utils.FakeAdaptiveMediaSource) ConcatenatingMediaSource(androidx.media3.exoplayer.source.ConcatenatingMediaSource) MediaSource(androidx.media3.exoplayer.source.MediaSource) FakeTimeline(androidx.media3.test.utils.FakeTimeline) TimelineWindowDefinition(androidx.media3.test.utils.FakeTimeline.TimelineWindowDefinition) ConcatenatingMediaSource(androidx.media3.exoplayer.source.ConcatenatingMediaSource) MediaPeriodId(androidx.media3.exoplayer.source.MediaSource.MediaPeriodId) ExoPlayerTestRunner(androidx.media3.test.utils.ExoPlayerTestRunner) FakeMediaPeriod(androidx.media3.test.utils.FakeMediaPeriod) MediaPeriod(androidx.media3.exoplayer.source.MediaPeriod) Nullable(androidx.annotation.Nullable) Test(org.junit.Test)

Aggregations

Nullable (androidx.annotation.Nullable)1 Timeline (androidx.media3.common.Timeline)1 TransferListener (androidx.media3.datasource.TransferListener)1 ClippingMediaSource (androidx.media3.exoplayer.source.ClippingMediaSource)1 CompositeMediaSource (androidx.media3.exoplayer.source.CompositeMediaSource)1 ConcatenatingMediaSource (androidx.media3.exoplayer.source.ConcatenatingMediaSource)1 MaskingMediaSource (androidx.media3.exoplayer.source.MaskingMediaSource)1 MediaPeriod (androidx.media3.exoplayer.source.MediaPeriod)1 MediaSource (androidx.media3.exoplayer.source.MediaSource)1 MediaPeriodId (androidx.media3.exoplayer.source.MediaSource.MediaPeriodId)1 SinglePeriodTimeline (androidx.media3.exoplayer.source.SinglePeriodTimeline)1 ServerSideAdInsertionMediaSource (androidx.media3.exoplayer.source.ads.ServerSideAdInsertionMediaSource)1 Allocator (androidx.media3.exoplayer.upstream.Allocator)1 ActionSchedule (androidx.media3.test.utils.ActionSchedule)1 PlayerRunnable (androidx.media3.test.utils.ActionSchedule.PlayerRunnable)1 ExoPlayerTestRunner (androidx.media3.test.utils.ExoPlayerTestRunner)1 FakeAdaptiveMediaSource (androidx.media3.test.utils.FakeAdaptiveMediaSource)1 FakeMediaPeriod (androidx.media3.test.utils.FakeMediaPeriod)1 FakeMediaSource (androidx.media3.test.utils.FakeMediaSource)1 FakeTimeline (androidx.media3.test.utils.FakeTimeline)1