use of androidx.media3.common.Player.Listener 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.Listener 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);
}
use of androidx.media3.common.Player.Listener in project media by androidx.
the class MediaControllerStateMaskingWithMediaSessionCompatTest method removeMediaItems_beforeCurrentMediaItemIndex_shiftsCurrentMediaItemIndex.
@Test
public void removeMediaItems_beforeCurrentMediaItemIndex_shiftsCurrentMediaItemIndex() throws Exception {
List<MediaItem> mediaItems = MediaTestUtils.createMediaItems(5);
List<QueueItem> queue = MediaUtils.convertToQueueItemList(mediaItems);
long testPosition = 200L;
int initialMediaItemIndex = 4;
MediaItem testCurrentMediaItem = mediaItems.get(initialMediaItemIndex);
session.setPlaybackState(new PlaybackStateCompat.Builder().setState(PlaybackStateCompat.STATE_PAUSED, testPosition, /* playbackSpeed= */
1.0f).setActiveQueueItemId(queue.get(initialMediaItemIndex).getQueueId()).build());
session.setQueue(queue);
int testFromIndex = 1;
int testToIndex = 3;
List<MediaItem> testMediaItems = new ArrayList<>(mediaItems.subList(0, testFromIndex));
testMediaItems.addAll(mediaItems.subList(testToIndex, mediaItems.size()));
int testCurrentMediaItemIndex = testMediaItems.indexOf(testCurrentMediaItem);
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.removeMediaItems(testFromIndex, testToIndex);
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);
}
use of androidx.media3.common.Player.Listener in project media by androidx.
the class MediaControllerStateMaskingWithMediaSessionCompatTest method stop.
@Test
public void stop() throws Exception {
long duration = 6000L;
long activeQueueItemId = 0;
long initialCurrentPosition = 3000L;
long initialBufferedPosition = duration;
int testPlaybackState = Player.STATE_IDLE;
long testCurrentPosition = 3000L;
long testBufferedPosition = testCurrentPosition;
int testBufferedPercentage = 50;
long testTotalBufferedDuration = testBufferedPosition - testCurrentPosition;
List<QueueItem> testQueue = MediaTestUtils.createQueueItems(3);
Events testEvents = new Events(new FlagSet.Builder().addAll(EVENT_PLAYBACK_STATE_CHANGED).build());
session.setPlaybackState(new PlaybackStateCompat.Builder().setActiveQueueItemId(activeQueueItemId).setBufferedPosition(initialBufferedPosition).setState(PlaybackStateCompat.STATE_PAUSED, /* position= */
initialCurrentPosition, /* playbackSpeed= */
1.0f).build());
session.setMetadata(new MediaMetadataCompat.Builder().putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration).build());
session.setQueue(testQueue);
MediaController controller = controllerTestRule.createController(session.getSessionToken());
CountDownLatch latch = new CountDownLatch(2);
AtomicInteger playbackStateFromCallbackRef = new AtomicInteger();
AtomicReference<Player.Events> onEventsRef = new AtomicReference<>();
Player.Listener listener = new Player.Listener() {
@Override
public void onPlaybackStateChanged(int playbackState) {
playbackStateFromCallbackRef.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 playbackStateFromGetterRef = new AtomicInteger();
AtomicReference<Timeline> timelineFromGetterRef = new AtomicReference<>();
AtomicLong currentPositionFromGetterRef = new AtomicLong();
AtomicLong bufferedPositionFromGetterRef = new AtomicLong();
AtomicInteger bufferedPercentageFromGetterRef = new AtomicInteger();
AtomicLong totalBufferedDurationFromGetterRef = new AtomicLong();
threadTestRule.getHandler().postAndSync(() -> {
controller.stop();
playbackStateFromGetterRef.set(controller.getPlaybackState());
timelineFromGetterRef.set(controller.getCurrentTimeline());
currentPositionFromGetterRef.set(controller.getCurrentPosition());
bufferedPositionFromGetterRef.set(controller.getBufferedPosition());
bufferedPercentageFromGetterRef.set(controller.getBufferedPercentage());
totalBufferedDurationFromGetterRef.set(controller.getTotalBufferedDuration());
});
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(playbackStateFromCallbackRef.get()).isEqualTo(testPlaybackState);
assertThat(onEventsRef.get()).isEqualTo(testEvents);
assertThat(playbackStateFromGetterRef.get()).isEqualTo(testPlaybackState);
assertThat(timelineFromGetterRef.get().getWindowCount()).isEqualTo(testQueue.size());
assertThat(currentPositionFromGetterRef.get()).isEqualTo(testCurrentPosition);
assertThat(bufferedPositionFromGetterRef.get()).isEqualTo(testBufferedPosition);
assertThat(bufferedPercentageFromGetterRef.get()).isEqualTo(testBufferedPercentage);
assertThat(totalBufferedDurationFromGetterRef.get()).isEqualTo(testTotalBufferedDuration);
}
use of androidx.media3.common.Player.Listener in project media by androidx.
the class MediaControllerStateMaskingWithMediaSessionCompatTest method seekTo_withNewMediaItemIndex.
@Test
public void seekTo_withNewMediaItemIndex() throws Exception {
List<MediaItem> mediaItems = MediaTestUtils.createMediaItems(3);
List<QueueItem> queue = MediaUtils.convertToQueueItemList(mediaItems);
long initialPosition = 8_000;
long initialBufferedPosition = 9_200;
int initialIndex = 0;
long testPosition = 9_000;
long testBufferedPosition = 0;
int testIndex = 1;
Events testEvents = new Events(new FlagSet.Builder().addAll(EVENT_POSITION_DISCONTINUITY, EVENT_PLAYBACK_STATE_CHANGED, EVENT_MEDIA_ITEM_TRANSITION).build());
session.setPlaybackState(new PlaybackStateCompat.Builder().setState(PlaybackStateCompat.STATE_PAUSED, initialPosition, /* playbackSpeed= */
1.0f).setBufferedPosition(initialBufferedPosition).setActiveQueueItemId(queue.get(initialIndex).getQueueId()).build());
session.setQueue(queue);
MediaController controller = controllerTestRule.createController(session.getSessionToken());
CountDownLatch latch = new CountDownLatch(4);
AtomicReference<PositionInfo> oldPositionInfoRef = new AtomicReference<>();
AtomicReference<PositionInfo> newPositionInfoRef = new AtomicReference<>();
AtomicInteger discontinuityReasonRef = new AtomicInteger();
AtomicInteger playbackStateRef = new AtomicInteger();
AtomicReference<MediaItem> mediaItemRef = new AtomicReference<>();
AtomicInteger mediaItemTransitionReasonRef = new AtomicInteger();
AtomicReference<Player.Events> onEventsRef = new AtomicReference<>();
Player.Listener listener = new Player.Listener() {
@Override
public void onPositionDiscontinuity(PositionInfo oldPosition, PositionInfo newPosition, @DiscontinuityReason int reason) {
oldPositionInfoRef.set(oldPosition);
newPositionInfoRef.set(newPosition);
discontinuityReasonRef.set(reason);
latch.countDown();
}
@Override
public void onPlaybackStateChanged(@State int playbackState) {
playbackStateRef.set(playbackState);
latch.countDown();
}
@Override
public void onMediaItemTransition(@Nullable MediaItem mediaItem, @MediaItemTransitionReason int reason) {
mediaItemRef.set(mediaItem);
mediaItemTransitionReasonRef.set(reason);
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();
threadTestRule.getHandler().postAndSync(() -> {
controller.seekTo(testIndex, testPosition);
currentPositionRef.set(controller.getCurrentPosition());
bufferedPositionRef.set(controller.getBufferedPosition());
});
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(oldPositionInfoRef.get().positionMs).isEqualTo(initialPosition);
assertThat(oldPositionInfoRef.get().mediaItemIndex).isEqualTo(initialIndex);
assertThat(newPositionInfoRef.get().positionMs).isEqualTo(testPosition);
assertThat(newPositionInfoRef.get().mediaItemIndex).isEqualTo(testIndex);
assertThat(discontinuityReasonRef.get()).isEqualTo(DISCONTINUITY_REASON_SEEK);
assertThat(playbackStateRef.get()).isEqualTo(STATE_BUFFERING);
assertThat(mediaItemRef.get()).isEqualTo(mediaItems.get(testIndex));
assertThat(mediaItemTransitionReasonRef.get()).isEqualTo(MEDIA_ITEM_TRANSITION_REASON_SEEK);
assertThat(onEventsRef.get()).isEqualTo(testEvents);
assertThat(currentPositionRef.get()).isEqualTo(testPosition);
assertThat(bufferedPositionRef.get()).isEqualTo(testBufferedPosition);
}
Aggregations