Search in sources :

Example 21 with Events

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

the class MediaControllerStateMaskingTest method removeMediaItems_removePreviousItemWithMultiplePeriods.

@Test
public void removeMediaItems_removePreviousItemWithMultiplePeriods() throws Exception {
    int initialMediaItemIndex = 1;
    int initialWindowFirstPeriodIndex = 2;
    int initialWindowLastPeriodIndex = 4;
    int initialPeriodIndex = 3;
    int testFromIndex = 0;
    int testToIndex = 1;
    int testMediaItemCount = 2;
    int prevWindowPeriodSize = 2;
    int testCurrentMediaItemIndex = 0;
    int testCurrentPeriodIndex = initialPeriodIndex - prevWindowPeriodSize;
    int testCurrentWindowFirstPeriodIndex = initialWindowFirstPeriodIndex - prevWindowPeriodSize;
    int testCurrentWindowLastPeriodIndex = initialWindowLastPeriodIndex - prevWindowPeriodSize;
    Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setTimeline(MediaTestUtils.createTimelineWithPeriodSizes(new int[] { prevWindowPeriodSize, 3, 2 })).setCurrentMediaItemIndex(initialMediaItemIndex).setCurrentPeriodIndex(initialPeriodIndex).build();
    remoteSession.setPlayer(playerConfig);
    MediaController controller = controllerTestRule.createController(remoteSession.getToken());
    CountDownLatch latch = new CountDownLatch(2);
    AtomicReference<Timeline> newTimelineRef = new AtomicReference<>();
    AtomicReference<Player.Events> onEventsRef = new AtomicReference<>();
    Player.Listener listener = new Player.Listener() {

        @Override
        public void onTimelineChanged(Timeline timeline, int reason) {
            newTimelineRef.set(timeline);
            latch.countDown();
        }

        @Override
        public void onEvents(Player player, Player.Events events) {
            onEventsRef.set(events);
            latch.countDown();
        }
    };
    threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
    AtomicInteger currentMediaItemIndexRef = new AtomicInteger();
    AtomicInteger currentPeriodIndexRef = new AtomicInteger();
    threadTestRule.getHandler().postAndSync(() -> {
        controller.removeMediaItems(/* fromIndex= */
        testFromIndex, /* toIndex= */
        testToIndex);
        currentMediaItemIndexRef.set(controller.getCurrentMediaItemIndex());
        currentPeriodIndexRef.set(controller.getCurrentPeriodIndex());
    });
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertTimeline(newTimelineRef.get(), testMediaItemCount, testCurrentMediaItemIndex, /* testFirstPeriodIndex= */
    testCurrentWindowFirstPeriodIndex, /* testLastPeriodIndex= */
    testCurrentWindowLastPeriodIndex);
    assertThat(onEventsRef.get().contains(Player.EVENT_TIMELINE_CHANGED)).isTrue();
    assertThat(currentMediaItemIndexRef.get()).isEqualTo(testCurrentMediaItemIndex);
    assertThat(currentPeriodIndexRef.get()).isEqualTo(testCurrentPeriodIndex);
}
Also used : Player(androidx.media3.common.Player) Bundle(android.os.Bundle) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) MediaTestUtils.createTimeline(androidx.media3.session.MediaTestUtils.createTimeline) Timeline(androidx.media3.common.Timeline) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 22 with Events

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

the class MediaControllerStateMaskingTest method setRepeatMode.

@Test
public void setRepeatMode() throws Exception {
    int testRepeatMode = REPEAT_MODE_ALL;
    Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setRepeatMode(REPEAT_MODE_ONE).build();
    remoteSession.setPlayer(playerConfig);
    MediaController controller = controllerTestRule.createController(remoteSession.getToken());
    CountDownLatch latch = new CountDownLatch(2);
    AtomicInteger repeatModeFromCallbackRef = new AtomicInteger();
    AtomicReference<Player.Events> onEventsRef = new AtomicReference<>();
    Player.Listener listener = new Player.Listener() {

        @Override
        public void onRepeatModeChanged(int repeatMode) {
            repeatModeFromCallbackRef.set(repeatMode);
            latch.countDown();
        }

        @Override
        public void onEvents(Player player, Player.Events events) {
            onEventsRef.set(events);
            latch.countDown();
        }
    };
    threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
    AtomicInteger repeatModeFromGetterRef = new AtomicInteger();
    threadTestRule.getHandler().postAndSync(() -> {
        controller.setRepeatMode(testRepeatMode);
        repeatModeFromGetterRef.set(controller.getRepeatMode());
    });
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(repeatModeFromCallbackRef.get()).isEqualTo(testRepeatMode);
    assertThat(onEventsRef.get().contains(Player.EVENT_REPEAT_MODE_CHANGED)).isTrue();
    assertThat(repeatModeFromGetterRef.get()).isEqualTo(testRepeatMode);
}
Also used : Player(androidx.media3.common.Player) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Bundle(android.os.Bundle) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 23 with Events

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

the class MediaControllerStateMaskingTest method assertMoveMediaItems_whenMovingBetweenWindowsWithMultiplePeriods.

private void assertMoveMediaItems_whenMovingBetweenWindowsWithMultiplePeriods(int initialMediaItemCount, int testFromIndex, int testToIndex, int testNewIndex, int testCurrentMediaItemIndex, int testCurrentWindowFirstPeriodIndex, int testCurrentWindowLastPeriodIndex, int testCurrentPeriodIndex) throws Exception {
    MediaController controller = controllerTestRule.createController(remoteSession.getToken());
    CountDownLatch latch = new CountDownLatch(2);
    AtomicReference<Timeline> newTimelineRef = new AtomicReference<>();
    AtomicReference<Player.Events> onEventsRef = new AtomicReference<>();
    Player.Listener listener = new Player.Listener() {

        @Override
        public void onTimelineChanged(Timeline timeline, int reason) {
            newTimelineRef.set(timeline);
            latch.countDown();
        }

        @Override
        public void onEvents(Player player, Player.Events events) {
            onEventsRef.set(events);
            latch.countDown();
        }
    };
    threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
    AtomicInteger currentMediaItemIndexRef = new AtomicInteger();
    AtomicInteger currentPeriodIndexRef = new AtomicInteger();
    threadTestRule.getHandler().postAndSync(() -> {
        controller.moveMediaItems(testFromIndex, testToIndex, testNewIndex);
        currentMediaItemIndexRef.set(controller.getCurrentMediaItemIndex());
        currentPeriodIndexRef.set(controller.getCurrentPeriodIndex());
    });
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertTimeline(newTimelineRef.get(), initialMediaItemCount, testCurrentMediaItemIndex, /* testFirstPeriodIndex= */
    testCurrentWindowFirstPeriodIndex, /* testLastPeriodIndex= */
    testCurrentWindowLastPeriodIndex);
    assertThat(onEventsRef.get().contains(Player.EVENT_TIMELINE_CHANGED)).isTrue();
    assertThat(currentMediaItemIndexRef.get()).isEqualTo(testCurrentMediaItemIndex);
    assertThat(currentPeriodIndexRef.get()).isEqualTo(testCurrentPeriodIndex);
}
Also used : MediaTestUtils.createTimeline(androidx.media3.session.MediaTestUtils.createTimeline) Timeline(androidx.media3.common.Timeline) Player(androidx.media3.common.Player) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 24 with Events

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

the class MediaControllerStateMaskingTest method removeMediaItems_currentItemNotRemoved.

@Test
public void removeMediaItems_currentItemNotRemoved() throws Exception {
    int initialMediaItemIndex = 1;
    String firstMediaId = "firstMediaId";
    String secondMediaId = "secondMediaId";
    String thirdMediaId = "thirdMediaId";
    String testCurrentMediaId = secondMediaId;
    int testFromIndex = 2;
    int testToIndex = 3;
    int testMediaItemCount = 2;
    int testCurrentMediaItemIndex = initialMediaItemIndex;
    int testNextMediaItemIndex = C.INDEX_UNSET;
    int testPreviousMediaItemIndex = testCurrentMediaItemIndex - 1;
    Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setTimeline(MediaTestUtils.createTimeline(createMediaItems(firstMediaId, secondMediaId, thirdMediaId))).setCurrentMediaItemIndex(initialMediaItemIndex).setCurrentPeriodIndex(initialMediaItemIndex).build();
    remoteSession.setPlayer(playerConfig);
    MediaController controller = controllerTestRule.createController(remoteSession.getToken());
    CountDownLatch latch = new CountDownLatch(2);
    AtomicReference<Timeline> newTimelineRef = new AtomicReference<>();
    AtomicReference<Player.Events> onEventsRef = new AtomicReference<>();
    Player.Listener listener = new Player.Listener() {

        @Override
        public void onTimelineChanged(Timeline timeline, int reason) {
            newTimelineRef.set(timeline);
            latch.countDown();
        }

        @Override
        public void onEvents(Player player, Player.Events events) {
            onEventsRef.set(events);
            latch.countDown();
        }
    };
    threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
    AtomicInteger currentMediaItemIndexRef = new AtomicInteger();
    AtomicInteger nextMediaItemIndexRef = new AtomicInteger();
    AtomicInteger previousMediaItemIndexRef = new AtomicInteger();
    threadTestRule.getHandler().postAndSync(() -> {
        controller.removeMediaItems(/* fromIndex= */
        testFromIndex, /* toIndex= */
        testToIndex);
        currentMediaItemIndexRef.set(controller.getCurrentMediaItemIndex());
        nextMediaItemIndexRef.set(controller.getNextMediaItemIndex());
        previousMediaItemIndexRef.set(controller.getPreviousMediaItemIndex());
    });
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertTimeline(newTimelineRef.get(), testMediaItemCount, testCurrentMediaItemIndex, /* testFirstPeriodIndex= */
    testCurrentMediaItemIndex, /* testLastPeriodIndex= */
    testCurrentMediaItemIndex);
    Window window = new Window();
    assertThat(newTimelineRef.get().getWindow(testCurrentMediaItemIndex, window).mediaItem.mediaId).isEqualTo(testCurrentMediaId);
    assertThat(onEventsRef.get().contains(Player.EVENT_TIMELINE_CHANGED)).isTrue();
    assertThat(currentMediaItemIndexRef.get()).isEqualTo(testCurrentMediaItemIndex);
    assertThat(nextMediaItemIndexRef.get()).isEqualTo(testNextMediaItemIndex);
    assertThat(previousMediaItemIndexRef.get()).isEqualTo(testPreviousMediaItemIndex);
}
Also used : Window(androidx.media3.common.Timeline.Window) Player(androidx.media3.common.Player) Bundle(android.os.Bundle) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) MediaTestUtils.createTimeline(androidx.media3.session.MediaTestUtils.createTimeline) Timeline(androidx.media3.common.Timeline) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 25 with Events

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

the class MediaControllerStateMaskingTest method addMediaItems_toEndOfTimeline.

@Test
public void addMediaItems_toEndOfTimeline() throws Exception {
    int initialMediaItemCount = 3;
    int initialMediaItemIndex = 2;
    int testMediaItemCount = 2;
    int testCurrentMediaItemIndex = initialMediaItemIndex;
    int testNextMediaItemIndex = testCurrentMediaItemIndex + 1;
    int testPreviousMediaItemIndex = testCurrentMediaItemIndex - 1;
    int testCurrentPeriodIndex = initialMediaItemIndex;
    Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setTimeline(MediaTestUtils.createTimeline(initialMediaItemCount)).setCurrentMediaItemIndex(initialMediaItemIndex).setCurrentPeriodIndex(initialMediaItemIndex).build();
    remoteSession.setPlayer(playerConfig);
    MediaController controller = controllerTestRule.createController(remoteSession.getToken());
    CountDownLatch latch = new CountDownLatch(2);
    AtomicReference<Timeline> newTimelineRef = new AtomicReference<>();
    AtomicReference<Player.Events> onEventsRef = new AtomicReference<>();
    Player.Listener listener = new Player.Listener() {

        @Override
        public void onTimelineChanged(Timeline timeline, int reason) {
            newTimelineRef.set(timeline);
            latch.countDown();
        }

        @Override
        public void onEvents(Player player, Player.Events events) {
            onEventsRef.set(events);
            latch.countDown();
        }
    };
    threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
    AtomicInteger currentMediaItemIndexRef = new AtomicInteger();
    AtomicInteger nextMediaItemIndexRef = new AtomicInteger();
    AtomicInteger previousMediaItemIndexRef = new AtomicInteger();
    AtomicInteger currentPeriodIndexRef = new AtomicInteger();
    threadTestRule.getHandler().postAndSync(() -> {
        controller.addMediaItems(createMediaItems(testMediaItemCount));
        currentMediaItemIndexRef.set(controller.getCurrentMediaItemIndex());
        nextMediaItemIndexRef.set(controller.getNextMediaItemIndex());
        previousMediaItemIndexRef.set(controller.getPreviousMediaItemIndex());
        currentPeriodIndexRef.set(controller.getCurrentPeriodIndex());
    });
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertTimeline(newTimelineRef.get(), initialMediaItemCount + testMediaItemCount, testCurrentMediaItemIndex, /* testFirstPeriodIndex= */
    testCurrentPeriodIndex, /* testLastPeriodIndex= */
    testCurrentPeriodIndex);
    assertThat(onEventsRef.get().contains(Player.EVENT_TIMELINE_CHANGED)).isTrue();
    assertThat(currentMediaItemIndexRef.get()).isEqualTo(testCurrentMediaItemIndex);
    assertThat(nextMediaItemIndexRef.get()).isEqualTo(testNextMediaItemIndex);
    assertThat(previousMediaItemIndexRef.get()).isEqualTo(testPreviousMediaItemIndex);
    assertThat(currentPeriodIndexRef.get()).isEqualTo(testCurrentPeriodIndex);
}
Also used : Player(androidx.media3.common.Player) Bundle(android.os.Bundle) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) MediaTestUtils.createTimeline(androidx.media3.session.MediaTestUtils.createTimeline) Timeline(androidx.media3.common.Timeline) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)65 Player (androidx.media3.common.Player)64 CountDownLatch (java.util.concurrent.CountDownLatch)58 AtomicReference (java.util.concurrent.atomic.AtomicReference)56 LargeTest (androidx.test.filters.LargeTest)52 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)46 Bundle (android.os.Bundle)34 Timeline (androidx.media3.common.Timeline)31 FlagSet (androidx.media3.common.FlagSet)24 MediaItem (androidx.media3.common.MediaItem)24 Events (androidx.media3.common.Player.Events)20 PositionInfo (androidx.media3.common.Player.PositionInfo)19 Nullable (androidx.annotation.Nullable)17 MediaTestUtils.createTimeline (androidx.media3.session.MediaTestUtils.createTimeline)17 ArrayList (java.util.ArrayList)17 AtomicLong (java.util.concurrent.atomic.AtomicLong)15 QueueItem (android.support.v4.media.session.MediaSessionCompat.QueueItem)12 Window (androidx.media3.common.Timeline.Window)8 PlaybackStateCompat (android.support.v4.media.session.PlaybackStateCompat)6 MediaMetadata (androidx.media3.common.MediaMetadata)6