Search in sources :

Example 96 with Player

use of androidx.media3.common.Player in project media by androidx.

the class ExoPlayerTest method removeMediaItem_current_notifiesAvailableCommandsChanged.

@Test
public void removeMediaItem_current_notifiesAvailableCommandsChanged() {
    Player.Commands defaultCommands = createWithDefaultCommands();
    Player.Commands commandsWithSeekToNextWindow = createWithDefaultCommands(COMMAND_SEEK_TO_NEXT_MEDIA_ITEM, COMMAND_SEEK_TO_NEXT);
    Player.Listener mockListener = mock(Player.Listener.class);
    ExoPlayer player = new TestExoPlayerBuilder(context).build();
    player.addListener(mockListener);
    player.addMediaSources(ImmutableList.of(new FakeMediaSource(), new FakeMediaSource()));
    verify(mockListener).onAvailableCommandsChanged(commandsWithSeekToNextWindow);
    // Check that there were no other calls to onAvailableCommandsChanged.
    verify(mockListener).onAvailableCommandsChanged(any());
    player.removeMediaItem(/* index= */
    0);
    verify(mockListener).onAvailableCommandsChanged(defaultCommands);
    verify(mockListener, times(2)).onAvailableCommandsChanged(any());
}
Also used : Listener(androidx.media3.common.Player.Listener) Player(androidx.media3.common.Player) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) TestExoPlayerBuilder(androidx.media3.test.utils.TestExoPlayerBuilder) Test(org.junit.Test)

Example 97 with Player

use of androidx.media3.common.Player in project media by androidx.

the class ExoPlayerTest method onPositionDiscontinuity_recursiveStateChange_mediaItemMaskingCorrect.

@Test
public void onPositionDiscontinuity_recursiveStateChange_mediaItemMaskingCorrect() throws Exception {
    ExoPlayer player = new TestExoPlayerBuilder(context).build();
    Player.Listener listener = mock(Player.Listener.class);
    MediaItem[] currentMediaItems = new MediaItem[2];
    int[] mediaItemCount = new int[2];
    player.addListener(new Listener() {

        @Override
        public void onPositionDiscontinuity(PositionInfo oldPosition, PositionInfo newPosition, int reason) {
            if (reason == Player.DISCONTINUITY_REASON_AUTO_TRANSITION) {
                mediaItemCount[0] = player.getMediaItemCount();
                currentMediaItems[0] = player.getCurrentMediaItem();
                // This is called before the second listener is called.
                player.removeMediaItem(/* index= */
                1);
            }
        }
    });
    player.addListener(new Listener() {

        @Override
        public void onPositionDiscontinuity(PositionInfo oldPosition, PositionInfo newPosition, int reason) {
            if (reason == Player.DISCONTINUITY_REASON_AUTO_TRANSITION) {
                mediaItemCount[1] = player.getMediaItemCount();
                currentMediaItems[1] = player.getCurrentMediaItem();
            }
        }
    });
    player.addListener(listener);
    player.setMediaSources(ImmutableList.of(createFakeMediaSource(/* id= */
    "id-0"), createFakeMediaSource(/* id= */
    "id-1")));
    player.prepare();
    player.play();
    TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_ENDED);
    ArgumentCaptor<PositionInfo> newPositionArgumentCaptor = ArgumentCaptor.forClass(PositionInfo.class);
    InOrder inOrder = inOrder(listener);
    inOrder.verify(listener).onPositionDiscontinuity(any(), newPositionArgumentCaptor.capture(), eq(Player.DISCONTINUITY_REASON_AUTO_TRANSITION));
    inOrder.verify(listener).onPositionDiscontinuity(any(), newPositionArgumentCaptor.capture(), eq(Player.DISCONTINUITY_REASON_REMOVE));
    // The state at auto-transition event time.
    assertThat(mediaItemCount[0]).isEqualTo(2);
    assertThat(currentMediaItems[0].localConfiguration.tag).isEqualTo("id-1");
    // The masked state after id-1 has been removed.
    assertThat(mediaItemCount[1]).isEqualTo(1);
    assertThat(currentMediaItems[1].localConfiguration.tag).isEqualTo("id-0");
    // PositionInfo reports the media item at event time.
    assertThat(newPositionArgumentCaptor.getAllValues().get(0).mediaItem.localConfiguration.tag).isEqualTo("id-1");
    assertThat(newPositionArgumentCaptor.getAllValues().get(1).mediaItem.localConfiguration.tag).isEqualTo("id-0");
    player.release();
}
Also used : Player(androidx.media3.common.Player) AnalyticsListener(androidx.media3.exoplayer.analytics.AnalyticsListener) TransferListener(androidx.media3.datasource.TransferListener) MediaSourceEventListener(androidx.media3.exoplayer.source.MediaSourceEventListener) Listener(androidx.media3.common.Player.Listener) DrmSessionEventListener(androidx.media3.exoplayer.drm.DrmSessionEventListener) InOrder(org.mockito.InOrder) Listener(androidx.media3.common.Player.Listener) MediaItem(androidx.media3.common.MediaItem) TestPlayerRunHelper.playUntilStartOfMediaItem(androidx.media3.test.utils.robolectric.TestPlayerRunHelper.playUntilStartOfMediaItem) PositionInfo(androidx.media3.common.Player.PositionInfo) TestExoPlayerBuilder(androidx.media3.test.utils.TestExoPlayerBuilder) Test(org.junit.Test)

Example 98 with Player

use of androidx.media3.common.Player in project media by androidx.

the class ExoPlayerTest method recursivePlayerChangesReportConsistentValuesForAllListeners.

@Test
public void recursivePlayerChangesReportConsistentValuesForAllListeners() throws Exception {
    // We add two listeners to the player. The first stops the player as soon as it's ready and both
    // record the state change events they receive.
    final AtomicReference<Player> playerReference = new AtomicReference<>();
    final List<Integer> playerListener1States = new ArrayList<>();
    final List<Integer> playerListener2States = new ArrayList<>();
    final Player.Listener playerListener1 = new Player.Listener() {

        @Override
        public void onPlaybackStateChanged(@Player.State int playbackState) {
            playerListener1States.add(playbackState);
            if (playbackState == Player.STATE_READY) {
                playerReference.get().stop(/* reset= */
                true);
            }
        }
    };
    final Player.Listener playerListener2 = new Player.Listener() {

        @Override
        public void onPlaybackStateChanged(@Player.State int playbackState) {
            playerListener2States.add(playbackState);
        }
    };
    ActionSchedule actionSchedule = new ActionSchedule.Builder(TAG).executeRunnable(new PlayerRunnable() {

        @Override
        public void run(ExoPlayer player) {
            playerReference.set(player);
            player.addListener(playerListener1);
            player.addListener(playerListener2);
        }
    }).build();
    new ExoPlayerTestRunner.Builder(context).setActionSchedule(actionSchedule).build().start().blockUntilEnded(TIMEOUT_MS);
    assertThat(playerListener1States).containsExactly(Player.STATE_BUFFERING, Player.STATE_READY, Player.STATE_IDLE).inOrder();
    assertThat(playerListener2States).containsExactly(Player.STATE_BUFFERING, Player.STATE_READY, Player.STATE_IDLE).inOrder();
}
Also used : Player(androidx.media3.common.Player) AnalyticsListener(androidx.media3.exoplayer.analytics.AnalyticsListener) TransferListener(androidx.media3.datasource.TransferListener) MediaSourceEventListener(androidx.media3.exoplayer.source.MediaSourceEventListener) Listener(androidx.media3.common.Player.Listener) DrmSessionEventListener(androidx.media3.exoplayer.drm.DrmSessionEventListener) ActionSchedule(androidx.media3.test.utils.ActionSchedule) PlayerRunnable(androidx.media3.test.utils.ActionSchedule.PlayerRunnable) TestExoPlayerBuilder(androidx.media3.test.utils.TestExoPlayerBuilder) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Listener(androidx.media3.common.Player.Listener) Test(org.junit.Test)

Example 99 with Player

use of androidx.media3.common.Player in project media by androidx.

the class ExoPlayerTest method experimentalEnableOffloadSchedulingWhileSleepingForOffload_isDisabled_renderingResumes.

@Test
public void experimentalEnableOffloadSchedulingWhileSleepingForOffload_isDisabled_renderingResumes() throws Exception {
    FakeSleepRenderer sleepRenderer = new FakeSleepRenderer(C.TRACK_TYPE_AUDIO).sleepOnNextRender();
    ExoPlayer player = new TestExoPlayerBuilder(context).setRenderers(sleepRenderer).build();
    Timeline timeline = new FakeTimeline();
    player.setMediaSource(new FakeMediaSource(timeline, ExoPlayerTestRunner.AUDIO_FORMAT));
    player.experimentalSetOffloadSchedulingEnabled(true);
    player.prepare();
    player.play();
    runUntilSleepingForOffload(player, /* expectedSleepForOffload= */
    true);
    // Force the player to exit offload sleep
    player.experimentalSetOffloadSchedulingEnabled(false);
    runUntilSleepingForOffload(player, /* expectedSleepForOffload= */
    false);
    assertThat(player.experimentalIsSleepingForOffload()).isFalse();
    runUntilPlaybackState(player, Player.STATE_ENDED);
}
Also used : Timeline(androidx.media3.common.Timeline) NoUidTimeline(androidx.media3.test.utils.NoUidTimeline) FakeTimeline(androidx.media3.test.utils.FakeTimeline) SinglePeriodTimeline(androidx.media3.exoplayer.source.SinglePeriodTimeline) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) FakeTimeline(androidx.media3.test.utils.FakeTimeline) TestExoPlayerBuilder(androidx.media3.test.utils.TestExoPlayerBuilder) Test(org.junit.Test)

Example 100 with Player

use of androidx.media3.common.Player in project media by androidx.

the class ExoPlayerTest method targetLiveOffsetInMedia_adjustsLiveOffsetToTargetOffset.

@Test
public void targetLiveOffsetInMedia_adjustsLiveOffsetToTargetOffset() throws Exception {
    long windowStartUnixTimeMs = 987_654_321_000L;
    long nowUnixTimeMs = windowStartUnixTimeMs + 20_000;
    ExoPlayer player = new TestExoPlayerBuilder(context).setClock(new FakeClock(/* initialTimeMs= */
    nowUnixTimeMs, /* isAutoAdvancing= */
    true)).build();
    Timeline timeline = new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
    1, /* id= */
    0, /* isSeekable= */
    true, /* isDynamic= */
    true, /* isLive= */
    true, /* isPlaceholder= */
    false, /* durationUs= */
    1000 * C.MICROS_PER_SECOND, /* defaultPositionUs= */
    8 * C.MICROS_PER_SECOND, /* windowOffsetInFirstPeriodUs= */
    Util.msToUs(windowStartUnixTimeMs), ImmutableList.of(AdPlaybackState.NONE), new MediaItem.Builder().setUri(Uri.EMPTY).setLiveConfiguration(new MediaItem.LiveConfiguration.Builder().setTargetOffsetMs(9_000).build()).build()));
    Player.Listener mockListener = mock(Player.Listener.class);
    player.addListener(mockListener);
    player.pause();
    player.setMediaSource(new FakeMediaSource(timeline));
    player.prepare();
    TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY);
    long liveOffsetAtStart = player.getCurrentLiveOffset();
    // Verify test setup (now = 20 seconds in live window, default start position = 8 seconds).
    assertThat(liveOffsetAtStart).isIn(Range.closed(11_900L, 12_100L));
    // Play until close to the end of the available live window.
    TestPlayerRunHelper.playUntilPosition(player, /* mediaItemIndex= */
    0, /* positionMs= */
    999_000);
    long liveOffsetAtEnd = player.getCurrentLiveOffset();
    player.release();
    // Assert that player adjusted live offset to the media value.
    assertThat(liveOffsetAtEnd).isIn(Range.closed(8_900L, 9_100L));
    // Assert that none of these playback speed changes were reported.
    verify(mockListener, never()).onPlaybackParametersChanged(any());
}
Also used : Player(androidx.media3.common.Player) FakeMediaSource(androidx.media3.test.utils.FakeMediaSource) FakeClock(androidx.media3.test.utils.FakeClock) TestExoPlayerBuilder(androidx.media3.test.utils.TestExoPlayerBuilder) Listener(androidx.media3.common.Player.Listener) Timeline(androidx.media3.common.Timeline) NoUidTimeline(androidx.media3.test.utils.NoUidTimeline) FakeTimeline(androidx.media3.test.utils.FakeTimeline) SinglePeriodTimeline(androidx.media3.exoplayer.source.SinglePeriodTimeline) FakeTimeline(androidx.media3.test.utils.FakeTimeline) MediaItem(androidx.media3.common.MediaItem) TestPlayerRunHelper.playUntilStartOfMediaItem(androidx.media3.test.utils.robolectric.TestPlayerRunHelper.playUntilStartOfMediaItem) TimelineWindowDefinition(androidx.media3.test.utils.FakeTimeline.TimelineWindowDefinition) TestExoPlayerBuilder(androidx.media3.test.utils.TestExoPlayerBuilder) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)347 FakeMediaSource (androidx.media3.test.utils.FakeMediaSource)185 Player (androidx.media3.common.Player)183 TestExoPlayerBuilder (androidx.media3.test.utils.TestExoPlayerBuilder)174 Timeline (androidx.media3.common.Timeline)137 FakeTimeline (androidx.media3.test.utils.FakeTimeline)127 CountDownLatch (java.util.concurrent.CountDownLatch)108 PlayerRunnable (androidx.media3.test.utils.ActionSchedule.PlayerRunnable)107 LargeTest (androidx.test.filters.LargeTest)97 AtomicReference (java.util.concurrent.atomic.AtomicReference)95 ActionSchedule (androidx.media3.test.utils.ActionSchedule)91 SinglePeriodTimeline (androidx.media3.exoplayer.source.SinglePeriodTimeline)89 NoUidTimeline (androidx.media3.test.utils.NoUidTimeline)89 Listener (androidx.media3.common.Player.Listener)85 MediaItem (androidx.media3.common.MediaItem)84 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)81 TimelineWindowDefinition (androidx.media3.test.utils.FakeTimeline.TimelineWindowDefinition)72 Nullable (androidx.annotation.Nullable)68 ExoPlayerTestRunner (androidx.media3.test.utils.ExoPlayerTestRunner)67 ConcatenatingMediaSource (androidx.media3.exoplayer.source.ConcatenatingMediaSource)65