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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations