use of androidx.media3.common.Player.Events in project media by androidx.
the class MediaControllerStateMaskingTest method setPlaylistMetadata.
@Test
public void setPlaylistMetadata() throws Exception {
MediaMetadata testPlaylistMetadata = new MediaMetadata.Builder().setTitle("test").build();
Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setPlaylistMetadata(MediaMetadata.EMPTY).build();
remoteSession.setPlayer(playerConfig);
MediaController controller = controllerTestRule.createController(remoteSession.getToken());
CountDownLatch latch = new CountDownLatch(2);
AtomicReference<MediaMetadata> playlistMetadataFromCallbackRef = new AtomicReference<>();
Player.Listener listener = new Player.Listener() {
@Override
public void onPlaylistMetadataChanged(MediaMetadata mediaMetadata) {
playlistMetadataFromCallbackRef.set(mediaMetadata);
latch.countDown();
}
@Override
public void onEvents(Player player, Player.Events events) {
latch.countDown();
}
};
threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
AtomicReference<MediaMetadata> playlistMetadataFromGetterRef = new AtomicReference<>();
threadTestRule.getHandler().postAndSync(() -> {
controller.setPlaylistMetadata(testPlaylistMetadata);
playlistMetadataFromGetterRef.set(controller.getPlaylistMetadata());
});
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(playlistMetadataFromCallbackRef.get()).isEqualTo(testPlaylistMetadata);
assertThat(playlistMetadataFromGetterRef.get()).isEqualTo(testPlaylistMetadata);
}
use of androidx.media3.common.Player.Events in project media by androidx.
the class MediaControllerStateMaskingTest method removeMediaItems_removeAllItems.
@Test
public void removeMediaItems_removeAllItems() throws Exception {
int initialMediaItemIndex = 1;
int initialPlaybackState = STATE_READY;
long initialCurrentPosition = 3_000;
String firstMediaId = "firstMediaId";
String secondMediaId = "secondMediaId";
String thirdMediaId = "thirdMediaId";
int testFromIndex = 0;
int testToIndex = 3;
int testMediaItemCount = 0;
int testCurrentMediaItemIndex = 0;
int testNextMediaItemIndex = C.INDEX_UNSET;
int testPreviousMediaItemIndex = C.INDEX_UNSET;
int testPlaybackState = STATE_ENDED;
long testCurrentPosition = 0;
Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setTimeline(MediaTestUtils.createTimeline(createMediaItems(firstMediaId, secondMediaId, thirdMediaId))).setCurrentMediaItemIndex(initialMediaItemIndex).setCurrentPeriodIndex(initialMediaItemIndex).setCurrentPosition(initialCurrentPosition).setPlaybackState(initialPlaybackState).build();
remoteSession.setPlayer(playerConfig);
MediaController controller = controllerTestRule.createController(remoteSession.getToken());
CountDownLatch latch = new CountDownLatch(4);
AtomicReference<Timeline> newTimelineRef = new AtomicReference<>();
AtomicReference<MediaItem> newMediaItemRef = new AtomicReference<>();
AtomicInteger newPlaybackStateRef = new AtomicInteger();
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 onMediaItemTransition(@Nullable MediaItem mediaItem, int reason) {
newMediaItemRef.set(mediaItem);
latch.countDown();
}
@Override
public void onPlaybackStateChanged(int playbackState) {
newPlaybackStateRef.set(playbackState);
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();
AtomicLong newCurrentPositionRef = new AtomicLong();
threadTestRule.getHandler().postAndSync(() -> {
controller.removeMediaItems(/* fromIndex= */
testFromIndex, /* toIndex= */
testToIndex);
currentMediaItemIndexRef.set(controller.getCurrentMediaItemIndex());
nextMediaItemIndexRef.set(controller.getNextMediaItemIndex());
previousMediaItemIndexRef.set(controller.getPreviousMediaItemIndex());
newCurrentPositionRef.set(controller.getCurrentPosition());
});
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertTimeline(newTimelineRef.get(), testMediaItemCount, /* ignored= */
C.INDEX_UNSET, /* ignored= */
C.INDEX_UNSET, /* ignored= */
C.INDEX_UNSET);
assertThat(newMediaItemRef.get()).isNull();
assertThat(newPlaybackStateRef.get()).isEqualTo(testPlaybackState);
assertThat(onEventsRef.get().contains(Player.EVENT_TIMELINE_CHANGED)).isTrue();
assertThat(onEventsRef.get().contains(Player.EVENT_MEDIA_ITEM_TRANSITION)).isTrue();
assertThat(onEventsRef.get().contains(Player.EVENT_PLAYBACK_STATE_CHANGED)).isTrue();
assertThat(currentMediaItemIndexRef.get()).isEqualTo(testCurrentMediaItemIndex);
assertThat(nextMediaItemIndexRef.get()).isEqualTo(testNextMediaItemIndex);
assertThat(previousMediaItemIndexRef.get()).isEqualTo(testPreviousMediaItemIndex);
assertThat(newCurrentPositionRef.get()).isEqualTo(testCurrentPosition);
}
use of androidx.media3.common.Player.Events in project media by androidx.
the class MediaControllerStateMaskingTest method seekTo_backwardsInSamePeriod.
@Test
public void seekTo_backwardsInSamePeriod() throws Exception {
long initialPosition = 8_000;
long initialBufferedPosition = 9_200;
long initialTotalBufferedDuration = 1_200;
int testMediaItemIndex = 0;
long testPosition = 1_000;
long testBufferedPosition = 1_000;
long testTotalBufferedDuration = 0;
Timeline testTimeline = createTimeline(1);
MediaItem testCurrentMediaItem = testTimeline.getWindow(testMediaItemIndex, new Window()).mediaItem;
Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setTimeline(testTimeline).setCurrentMediaItemIndex(testMediaItemIndex).setCurrentPeriodIndex(testMediaItemIndex).setCurrentPosition(initialPosition).setContentPosition(initialPosition).setBufferedPosition(initialBufferedPosition).setTotalBufferedDuration(initialTotalBufferedDuration).build();
remoteSession.setPlayer(playerConfig);
MediaController controller = controllerTestRule.createController(remoteSession.getToken());
CountDownLatch latch = new CountDownLatch(2);
AtomicReference<PositionInfo> newPositionInfoRef = new AtomicReference<>();
AtomicReference<Player.Events> onEventsRef = new AtomicReference<>();
Player.Listener listener = new Player.Listener() {
@Override
public void onPositionDiscontinuity(PositionInfo oldPosition, PositionInfo newPosition, int reason) {
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));
AtomicLong currentPositionRef = new AtomicLong();
AtomicLong bufferedPositionRef = new AtomicLong();
AtomicLong totalBufferedDurationRef = new AtomicLong();
AtomicReference<MediaItem> currentMediaItemRef = new AtomicReference<>();
threadTestRule.getHandler().postAndSync(() -> {
controller.seekTo(testPosition);
currentPositionRef.set(controller.getCurrentPosition());
bufferedPositionRef.set(controller.getBufferedPosition());
totalBufferedDurationRef.set(controller.getTotalBufferedDuration());
currentMediaItemRef.set(controller.getCurrentMediaItem());
});
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(newPositionInfoRef.get().positionMs).isEqualTo(testPosition);
assertThat(onEventsRef.get().contains(Player.EVENT_POSITION_DISCONTINUITY)).isTrue();
assertThat(currentPositionRef.get()).isEqualTo(testPosition);
assertThat(bufferedPositionRef.get()).isEqualTo(testBufferedPosition);
assertThat(totalBufferedDurationRef.get()).isEqualTo(testTotalBufferedDuration);
assertThat(currentMediaItemRef.get()).isEqualTo(testCurrentMediaItem);
}
use of androidx.media3.common.Player.Events in project media by androidx.
the class MediaControllerStateMaskingTest method setPlayWhenReady.
@Test
public void setPlayWhenReady() throws Exception {
boolean testPlayWhenReady = true;
@Player.PlaybackSuppressionReason int testReason = Player.PLAYBACK_SUPPRESSION_REASON_NONE;
boolean testIsPlaying = true;
Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setPlaybackState(Player.STATE_READY).setPlayWhenReady(false).setPlaybackSuppressionReason(Player.PLAYBACK_SUPPRESSION_REASON_TRANSIENT_AUDIO_FOCUS_LOSS).build();
remoteSession.setPlayer(playerConfig);
MediaController controller = controllerTestRule.createController(remoteSession.getToken());
CountDownLatch latch = new CountDownLatch(4);
AtomicBoolean playWhenReadyFromCallbackRef = new AtomicBoolean();
AtomicInteger playbackSuppressionReasonFromCallbackRef = new AtomicInteger();
AtomicBoolean isPlayingFromCallbackRef = new AtomicBoolean();
AtomicReference<Player.Events> onEventsRef = new AtomicReference<>();
Player.Listener listener = new Player.Listener() {
@Override
public void onPlayWhenReadyChanged(boolean playWhenReady, @Player.PlayWhenReadyChangeReason int reason) {
playWhenReadyFromCallbackRef.set(playWhenReady);
latch.countDown();
}
@Override
public void onPlaybackSuppressionReasonChanged(int playbackSuppressionReason) {
playbackSuppressionReasonFromCallbackRef.set(playbackSuppressionReason);
latch.countDown();
}
@Override
public void onIsPlayingChanged(boolean isPlaying) {
isPlayingFromCallbackRef.set(isPlaying);
latch.countDown();
}
@Override
public void onEvents(Player player, Player.Events events) {
onEventsRef.set(events);
latch.countDown();
}
};
threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
AtomicBoolean playWhenReadyFromGetterRef = new AtomicBoolean();
AtomicInteger playbackSuppressionReasonFromGetterRef = new AtomicInteger();
AtomicBoolean isPlayingFromGetterRef = new AtomicBoolean();
threadTestRule.getHandler().postAndSync(() -> {
controller.setPlayWhenReady(testPlayWhenReady);
playWhenReadyFromGetterRef.set(controller.getPlayWhenReady());
playbackSuppressionReasonFromGetterRef.set(controller.getPlaybackSuppressionReason());
isPlayingFromGetterRef.set(controller.isPlaying());
});
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(playWhenReadyFromCallbackRef.get()).isEqualTo(testPlayWhenReady);
assertThat(playbackSuppressionReasonFromCallbackRef.get()).isEqualTo(testReason);
assertThat(isPlayingFromCallbackRef.get()).isEqualTo(testIsPlaying);
assertThat(onEventsRef.get().contains(Player.EVENT_PLAY_WHEN_READY_CHANGED)).isTrue();
assertThat(onEventsRef.get().contains(Player.EVENT_PLAYBACK_SUPPRESSION_REASON_CHANGED)).isTrue();
assertThat(onEventsRef.get().contains(Player.EVENT_IS_PLAYING_CHANGED)).isTrue();
assertThat(playWhenReadyFromGetterRef.get()).isEqualTo(testPlayWhenReady);
assertThat(playbackSuppressionReasonFromGetterRef.get()).isEqualTo(testReason);
assertThat(isPlayingFromGetterRef.get()).isEqualTo(testIsPlaying);
}
use of androidx.media3.common.Player.Events in project media by androidx.
the class MediaControllerStateMaskingWithMediaSessionCompatTest method moveMediaItems.
@Test
public void moveMediaItems() throws Exception {
List<MediaItem> mediaItems = MediaTestUtils.createMediaItems(5);
List<QueueItem> queue = MediaUtils.convertToQueueItemList(mediaItems);
long testPosition = 200L;
int testCurrentMediaItemIndex = 0;
MediaItem testCurrentMediaItem = mediaItems.get(testCurrentMediaItemIndex);
session.setPlaybackState(new PlaybackStateCompat.Builder().setState(PlaybackStateCompat.STATE_PAUSED, testPosition, /* playbackSpeed= */
1.0f).setActiveQueueItemId(queue.get(testCurrentMediaItemIndex).getQueueId()).build());
session.setQueue(queue);
int testFromIndex = 1;
int testToIndex = 3;
int testNewIndex = 2;
List<MediaItem> testMediaItems = new ArrayList<>(mediaItems);
Util.moveItems(testMediaItems, testFromIndex, testToIndex, testNewIndex);
Events testEvents = new Events(new FlagSet.Builder().addAll(EVENT_TIMELINE_CHANGED).build());
MediaController controller = controllerTestRule.createController(session.getSessionToken());
CountDownLatch latch = new CountDownLatch(2);
AtomicReference<Timeline> timelineFromParamRef = new AtomicReference<>();
AtomicInteger timelineChangedReasonRef = new AtomicInteger();
AtomicReference<Player.Events> onEventsRef = new AtomicReference<>();
Player.Listener listener = new Player.Listener() {
@Override
public void onTimelineChanged(Timeline timeline, @Player.TimelineChangeReason int reason) {
timelineFromParamRef.set(timeline);
timelineChangedReasonRef.set(reason);
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();
AtomicReference<MediaItem> currentMediaItemRef = new AtomicReference<>();
AtomicReference<Timeline> timelineFromGetterRef = new AtomicReference<>();
threadTestRule.getHandler().postAndSync(() -> {
controller.moveMediaItems(testFromIndex, testToIndex, testNewIndex);
currentMediaItemIndexRef.set(controller.getCurrentMediaItemIndex());
currentMediaItemRef.set(controller.getCurrentMediaItem());
timelineFromGetterRef.set(controller.getCurrentTimeline());
});
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
MediaTestUtils.assertTimelineContains(timelineFromParamRef.get(), testMediaItems);
assertThat(timelineChangedReasonRef.get()).isEqualTo(TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED);
assertThat(onEventsRef.get()).isEqualTo(testEvents);
assertThat(currentMediaItemIndexRef.get()).isEqualTo(testCurrentMediaItemIndex);
assertThat(currentMediaItemRef.get()).isEqualTo(testCurrentMediaItem);
MediaTestUtils.assertTimelineContains(timelineFromGetterRef.get(), testMediaItems);
}
Aggregations