Search in sources :

Example 11 with Events

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

the class MediaControllerMediaSessionCompatCallbackAggregationTest method getters_withValidQueueAndQueueIdAndMetadata.

@Test
public void getters_withValidQueueAndQueueIdAndMetadata() throws Exception {
    int testSize = 3;
    List<MediaItem> testMediaItems = MediaTestUtils.createMediaItems(testSize);
    List<QueueItem> testQueue = MediaUtils.convertToQueueItemList(testMediaItems);
    int testMediaItemIndex = 1;
    MediaMetadataCompat testMediaMetadataCompat = createMediaMetadataCompat();
    @RatingCompat.Style int testRatingType = RatingCompat.RATING_HEART;
    MediaMetadata testMediaMetadata = MediaUtils.convertToMediaMetadata(testMediaMetadataCompat, testRatingType);
    MediaItem testCurrentMediaItem = new MediaItem.Builder().setMediaId(testMediaItems.get(testMediaItemIndex).mediaId).setMediaMetadata(testMediaMetadata).build();
    Events testEvents = new Events(new FlagSet.Builder().addAll(EVENT_MEDIA_ITEM_TRANSITION, EVENT_MEDIA_METADATA_CHANGED, EVENT_POSITION_DISCONTINUITY, EVENT_TIMELINE_CHANGED).build());
    MediaController controller = controllerTestRule.createController(session.getSessionToken());
    CountDownLatch latch = new CountDownLatch(5);
    AtomicReference<Timeline> timelineRef = new AtomicReference<>();
    AtomicInteger timelineChangeReasonRef = new AtomicInteger();
    AtomicReference<MediaItem> mediaItemRef = new AtomicReference<>();
    AtomicInteger mediaItemTransitionReasonRef = new AtomicInteger();
    AtomicReference<PositionInfo> positionInfoRef = new AtomicReference<>();
    AtomicInteger discontinuityReasonRef = new AtomicInteger();
    AtomicReference<MediaMetadata> metadataRef = new AtomicReference<>();
    AtomicReference<Events> eventsRef = new AtomicReference<>();
    Player.Listener listener = new Player.Listener() {

        @Override
        public void onTimelineChanged(Timeline timeline, @Player.TimelineChangeReason int reason) {
            timelineRef.set(timeline);
            timelineChangeReasonRef.set(reason);
            latch.countDown();
        }

        @Override
        public void onMediaItemTransition(@Nullable MediaItem mediaItem, @Player.MediaItemTransitionReason int reason) {
            mediaItemRef.set(mediaItem);
            mediaItemTransitionReasonRef.set(reason);
            latch.countDown();
        }

        @Override
        public void onPositionDiscontinuity(PositionInfo oldPosition, PositionInfo newPosition, @Player.DiscontinuityReason int reason) {
            positionInfoRef.set(newPosition);
            discontinuityReasonRef.set(reason);
            latch.countDown();
        }

        @Override
        public void onMediaMetadataChanged(MediaMetadata mediaMetadata) {
            metadataRef.set(mediaMetadata);
            latch.countDown();
        }

        @Override
        public void onEvents(Player player, Events events) {
            eventsRef.set(events);
            latch.countDown();
        }
    };
    threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
    session.setQueue(testQueue);
    session.setPlaybackState(new PlaybackStateCompat.Builder().setActiveQueueItemId(testQueue.get(testMediaItemIndex).getQueueId()).build());
    session.setMetadata(testMediaMetadataCompat);
    session.setRatingType(testRatingType);
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(mediaItemRef.get()).isEqualTo(testCurrentMediaItem);
    for (int i = 0; i < timelineRef.get().getWindowCount(); i++) {
        assertThat(timelineRef.get().getWindow(i, new Window()).mediaItem).isEqualTo(i == testMediaItemIndex ? testCurrentMediaItem : testMediaItems.get(i));
    }
    assertThat(timelineChangeReasonRef.get()).isEqualTo(TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED);
    assertThat(mediaItemTransitionReasonRef.get()).isEqualTo(Player.MEDIA_ITEM_TRANSITION_REASON_PLAYLIST_CHANGED);
    assertThat(positionInfoRef.get().mediaItemIndex).isEqualTo(testMediaItemIndex);
    assertThat(discontinuityReasonRef.get()).isEqualTo(DISCONTINUITY_REASON_AUTO_TRANSITION);
    assertThat(metadataRef.get()).isEqualTo(testMediaMetadata);
    assertThat(eventsRef.get()).isEqualTo(testEvents);
    Timeline currentTimeline = threadTestRule.getHandler().postAndSync(controller::getCurrentTimeline);
    assertThat(currentTimeline).isEqualTo(timelineRef.get());
    int currentMediaItemIndex = threadTestRule.getHandler().postAndSync(controller::getCurrentMediaItemIndex);
    assertThat(currentMediaItemIndex).isEqualTo(testMediaItemIndex);
    MediaItem currentMediaItem = threadTestRule.getHandler().postAndSync(controller::getCurrentMediaItem);
    assertThat(currentMediaItem).isEqualTo(testCurrentMediaItem);
    MediaMetadata currentMediaMetadata = threadTestRule.getHandler().postAndSync(controller::getMediaMetadata);
    assertThat(currentMediaMetadata).isEqualTo(currentMediaItem.mediaMetadata);
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat) Events(androidx.media3.common.Player.Events) PositionInfo(androidx.media3.common.Player.PositionInfo) QueueItem(android.support.v4.media.session.MediaSessionCompat.QueueItem) Window(androidx.media3.common.Timeline.Window) Player(androidx.media3.common.Player) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) FlagSet(androidx.media3.common.FlagSet) Timeline(androidx.media3.common.Timeline) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MediaItem(androidx.media3.common.MediaItem) MediaMetadata(androidx.media3.common.MediaMetadata) Nullable(androidx.annotation.Nullable) MediumTest(androidx.test.filters.MediumTest) Test(org.junit.Test)

Example 12 with Events

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

the class MediaControllerMediaSessionCompatCallbackAggregationTest method getters_withValidQueueAndQueueIdWithoutMetadata.

@Test
public void getters_withValidQueueAndQueueIdWithoutMetadata() throws Exception {
    int testSize = 3;
    List<MediaItem> testMediaItems = MediaTestUtils.createMediaItems(testSize);
    List<QueueItem> testQueue = MediaUtils.convertToQueueItemList(testMediaItems);
    @RatingCompat.Style int testRatingType = RatingCompat.RATING_HEART;
    Events testEvents = new Events(new FlagSet.Builder().addAll(EVENT_MEDIA_ITEM_TRANSITION, EVENT_POSITION_DISCONTINUITY, EVENT_TIMELINE_CHANGED).build());
    int testMediaItemIndex = 1;
    MediaController controller = controllerTestRule.createController(session.getSessionToken());
    CountDownLatch latch = new CountDownLatch(4);
    AtomicReference<Timeline> timelineRef = new AtomicReference<>();
    AtomicInteger timelineChangeReasonRef = new AtomicInteger();
    AtomicReference<MediaItem> mediaItemRef = new AtomicReference<>();
    AtomicInteger mediaItemTransitionReasonRef = new AtomicInteger();
    AtomicReference<PositionInfo> positionInfoRef = new AtomicReference<>();
    AtomicInteger discontinuityReasonRef = new AtomicInteger();
    AtomicReference<Events> eventsRef = new AtomicReference<>();
    Player.Listener listener = new Player.Listener() {

        @Override
        public void onTimelineChanged(Timeline timeline, @Player.TimelineChangeReason int reason) {
            timelineRef.set(timeline);
            timelineChangeReasonRef.set(reason);
            latch.countDown();
        }

        @Override
        public void onMediaItemTransition(@Nullable MediaItem mediaItem, @Player.MediaItemTransitionReason int reason) {
            mediaItemRef.set(mediaItem);
            mediaItemTransitionReasonRef.set(reason);
            latch.countDown();
        }

        @Override
        public void onPositionDiscontinuity(PositionInfo oldPosition, PositionInfo newPosition, @Player.DiscontinuityReason int reason) {
            positionInfoRef.set(newPosition);
            discontinuityReasonRef.set(reason);
            latch.countDown();
        }

        @Override
        public void onEvents(Player player, Events events) {
            eventsRef.set(events);
            latch.countDown();
        }
    };
    threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
    session.setQueue(testQueue);
    session.setPlaybackState(new PlaybackStateCompat.Builder().setActiveQueueItemId(testQueue.get(testMediaItemIndex).getQueueId()).build());
    session.setRatingType(testRatingType);
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertTimelineEqualsToMediaItems(timelineRef.get(), testMediaItems);
    assertThat(timelineChangeReasonRef.get()).isEqualTo(TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED);
    assertThat(mediaItemRef.get()).isEqualTo(testMediaItems.get(testMediaItemIndex));
    assertThat(mediaItemTransitionReasonRef.get()).isEqualTo(Player.MEDIA_ITEM_TRANSITION_REASON_PLAYLIST_CHANGED);
    assertThat(positionInfoRef.get().mediaItemIndex).isEqualTo(testMediaItemIndex);
    assertThat(discontinuityReasonRef.get()).isEqualTo(DISCONTINUITY_REASON_AUTO_TRANSITION);
    assertThat(eventsRef.get()).isEqualTo(testEvents);
    Timeline currentTimeline = threadTestRule.getHandler().postAndSync(controller::getCurrentTimeline);
    assertThat(currentTimeline).isEqualTo(timelineRef.get());
    int currentMediaItemIndex = threadTestRule.getHandler().postAndSync(controller::getCurrentMediaItemIndex);
    assertThat(currentMediaItemIndex).isEqualTo(testMediaItemIndex);
    MediaItem currentMediaItem = threadTestRule.getHandler().postAndSync(controller::getCurrentMediaItem);
    assertThat(currentMediaItem).isEqualTo(testMediaItems.get(testMediaItemIndex));
    MediaMetadata currentMediaMetadata = threadTestRule.getHandler().postAndSync(controller::getMediaMetadata);
    assertThat(currentMediaMetadata).isEqualTo(currentMediaItem.mediaMetadata);
}
Also used : PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat) Events(androidx.media3.common.Player.Events) PositionInfo(androidx.media3.common.Player.PositionInfo) QueueItem(android.support.v4.media.session.MediaSessionCompat.QueueItem) Player(androidx.media3.common.Player) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) FlagSet(androidx.media3.common.FlagSet) Timeline(androidx.media3.common.Timeline) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MediaItem(androidx.media3.common.MediaItem) MediaMetadata(androidx.media3.common.MediaMetadata) Nullable(androidx.annotation.Nullable) MediumTest(androidx.test.filters.MediumTest) Test(org.junit.Test)

Example 13 with Events

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

the class MediaControllerStateMaskingTest method seekToPreviousMediaItem.

@Test
public void seekToPreviousMediaItem() throws Exception {
    int initialMediaItemIndex = 1;
    String firstMediaId = "firstMediaId";
    String secondMediaId = "secondMediaId";
    String thirdMediaId = "thirdMediaId";
    String testCurrentMediaId = firstMediaId;
    int testMediaItemIndex = 0;
    Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setTimeline(MediaTestUtils.createTimeline(MediaTestUtils.createMediaItems(firstMediaId, secondMediaId, thirdMediaId))).setCurrentMediaItemIndex(initialMediaItemIndex).setCurrentPeriodIndex(initialMediaItemIndex).build();
    remoteSession.setPlayer(playerConfig);
    MediaController controller = controllerTestRule.createController(remoteSession.getToken());
    CountDownLatch latch = new CountDownLatch(3);
    AtomicReference<MediaItem> newMediaItemRef = new AtomicReference<>();
    AtomicReference<PositionInfo> oldPositionInfoRef = new AtomicReference<>();
    AtomicReference<PositionInfo> newPositionInfoRef = new AtomicReference<>();
    AtomicReference<Player.Events> onEventsRef = new AtomicReference<>();
    Player.Listener listener = new Player.Listener() {

        @Override
        public void onMediaItemTransition(@Nullable MediaItem mediaItem, int reason) {
            newMediaItemRef.set(mediaItem);
            latch.countDown();
        }

        @Override
        public void onPositionDiscontinuity(PositionInfo oldPosition, PositionInfo newPosition, int reason) {
            oldPositionInfoRef.set(oldPosition);
            newPositionInfoRef.set(newPosition);
            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();
    threadTestRule.getHandler().postAndSync(() -> {
        controller.seekToPreviousMediaItem();
        currentMediaItemIndexRef.set(controller.getCurrentMediaItemIndex());
    });
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(newMediaItemRef.get().mediaId).isEqualTo(testCurrentMediaId);
    assertThat(oldPositionInfoRef.get().mediaItemIndex).isEqualTo(initialMediaItemIndex);
    assertThat(newPositionInfoRef.get().mediaItemIndex).isEqualTo(testMediaItemIndex);
    assertThat(onEventsRef.get().contains(Player.EVENT_MEDIA_ITEM_TRANSITION)).isTrue();
    assertThat(onEventsRef.get().contains(Player.EVENT_POSITION_DISCONTINUITY)).isTrue();
    assertThat(currentMediaItemIndexRef.get()).isEqualTo(testMediaItemIndex);
}
Also used : Player(androidx.media3.common.Player) Bundle(android.os.Bundle) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MediaItem(androidx.media3.common.MediaItem) PositionInfo(androidx.media3.common.Player.PositionInfo) Nullable(androidx.annotation.Nullable) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 14 with Events

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

the class MediaControllerStateMaskingTest method setDeviceVolume.

@Test
public void setDeviceVolume() throws Exception {
    int testDeviceVolume = 2;
    Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setDeviceVolume(0).build();
    remoteSession.setPlayer(playerConfig);
    MediaController controller = controllerTestRule.createController(remoteSession.getToken());
    CountDownLatch latch = new CountDownLatch(2);
    AtomicInteger deviceVolumeFromCallbackRef = new AtomicInteger();
    Player.Listener listener = new Player.Listener() {

        @Override
        public void onDeviceVolumeChanged(int volume, boolean muted) {
            deviceVolumeFromCallbackRef.set(volume);
            latch.countDown();
        }

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

Example 15 with Events

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

the class MediaControllerStateMaskingTest method setPlaybackParameters.

@Test
public void setPlaybackParameters() throws Exception {
    PlaybackParameters testPlaybackParameters = new PlaybackParameters(2f, 2f);
    Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setPlaybackParameters(PlaybackParameters.DEFAULT).build();
    remoteSession.setPlayer(playerConfig);
    MediaController controller = controllerTestRule.createController(remoteSession.getToken());
    CountDownLatch latch = new CountDownLatch(2);
    AtomicReference<PlaybackParameters> playbackParametersFromCallbackRef = new AtomicReference<>();
    AtomicReference<Player.Events> onEventsRef = new AtomicReference<>();
    Player.Listener listener = new Player.Listener() {

        @Override
        public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
            playbackParametersFromCallbackRef.set(playbackParameters);
            latch.countDown();
        }

        @Override
        public void onEvents(Player player, Player.Events events) {
            onEventsRef.set(events);
            latch.countDown();
        }
    };
    threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
    AtomicReference<PlaybackParameters> playbackParametersFromGetterRef = new AtomicReference<>();
    threadTestRule.getHandler().postAndSync(() -> {
        controller.setPlaybackParameters(testPlaybackParameters);
        playbackParametersFromGetterRef.set(controller.getPlaybackParameters());
    });
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(playbackParametersFromCallbackRef.get()).isEqualTo(testPlaybackParameters);
    assertThat(onEventsRef.get().contains(Player.EVENT_PLAYBACK_PARAMETERS_CHANGED)).isTrue();
    assertThat(playbackParametersFromGetterRef.get()).isEqualTo(testPlaybackParameters);
}
Also used : Player(androidx.media3.common.Player) Bundle(android.os.Bundle) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) PlaybackParameters(androidx.media3.common.PlaybackParameters) 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