use of androidx.media3.common.Player.State in project media by androidx.
the class MediaControllerCompatCallbackWithMediaSessionTest method playerError_notified.
@Test
public void playerError_notified() throws Exception {
PlaybackException testPlayerError = new PlaybackException(/* messaage= */
"player error", /* cause= */
null, PlaybackException.ERROR_CODE_UNSPECIFIED);
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<PlaybackStateCompat> playbackStateCompatRef = new AtomicReference<>();
MediaControllerCompat.Callback callback = new MediaControllerCompat.Callback() {
@Override
public void onPlaybackStateChanged(PlaybackStateCompat state) {
playbackStateCompatRef.set(state);
latch.countDown();
}
};
controllerCompat.registerCallback(callback, handler);
session.getMockPlayer().notifyPlayerError(testPlayerError);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
PlaybackStateCompat state = playbackStateCompatRef.get();
assertPlaybackStateCompatErrorEquals(state, testPlayerError);
}
use of androidx.media3.common.Player.State in project media by androidx.
the class MediaControllerCompatCallbackWithMediaSessionTest method onPlaybackParametersChanged_notifiesPlaybackStateCompatChanges.
@Test
public void onPlaybackParametersChanged_notifiesPlaybackStateCompatChanges() throws Exception {
PlaybackParameters playbackParameters = new PlaybackParameters(/* speed= */
1.5f);
AtomicReference<PlaybackStateCompat> playbackStateRef = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
MediaControllerCompat.Callback callback = new MediaControllerCompat.Callback() {
@Override
public void onPlaybackStateChanged(PlaybackStateCompat state) {
playbackStateRef.set(state);
latch.countDown();
}
};
controllerCompat.registerCallback(callback, handler);
session.getMockPlayer().notifyPlaybackParametersChanged(playbackParameters);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(playbackStateRef.get().getPlaybackSpeed()).isWithin(EPSILON).of(playbackParameters.speed);
assertThat(controllerCompat.getPlaybackState().getPlaybackSpeed()).isWithin(EPSILON).of(playbackParameters.speed);
}
use of androidx.media3.common.Player.State in project media by androidx.
the class MediaControllerCompatCallbackWithMediaSessionTest method setPlayer_isNotified.
@Test
public void setPlayer_isNotified() throws Exception {
@State int testState = STATE_READY;
boolean testPlayWhenReady = true;
long testDurationMs = 200;
long testCurrentPositionMs = 11;
long testBufferedPositionMs = 100;
PlaybackParameters playbackParameters = new PlaybackParameters(/* speed= */
1.5f);
int testItemIndex = 0;
List<MediaItem> testMediaItems = MediaTestUtils.createMediaItems(/* size= */
3);
testMediaItems.set(testItemIndex, new MediaItem.Builder().setMediaId(testMediaItems.get(testItemIndex).mediaId).setMediaMetadata(new MediaMetadata.Builder().setUserRating(new HeartRating(/* isHeart= */
true)).build()).build());
Timeline testTimeline = new PlaylistTimeline(testMediaItems);
String testPlaylistTitle = "testPlaylistTitle";
MediaMetadata testPlaylistMetadata = new MediaMetadata.Builder().setTitle(testPlaylistTitle).build();
boolean testShuffleModeEnabled = true;
@RepeatMode int testRepeatMode = Player.REPEAT_MODE_ONE;
AtomicReference<PlaybackStateCompat> playbackStateRef = new AtomicReference<>();
AtomicReference<MediaMetadataCompat> metadataRef = new AtomicReference<>();
AtomicReference<CharSequence> queueTitleRef = new AtomicReference<>();
AtomicInteger shuffleModeRef = new AtomicInteger();
AtomicInteger repeatModeRef = new AtomicInteger();
CountDownLatch latchForPlaybackState = new CountDownLatch(1);
CountDownLatch latchForMetadata = new CountDownLatch(1);
CountDownLatch latchForQueue = new CountDownLatch(2);
CountDownLatch latchForShuffleMode = new CountDownLatch(1);
CountDownLatch latchForRepeatMode = new CountDownLatch(1);
MediaControllerCompat.Callback callback = new MediaControllerCompat.Callback() {
@Override
public void onPlaybackStateChanged(PlaybackStateCompat state) {
playbackStateRef.set(state);
latchForPlaybackState.countDown();
}
@Override
public void onMetadataChanged(MediaMetadataCompat metadata) {
metadataRef.set(metadata);
latchForMetadata.countDown();
}
@Override
public void onQueueChanged(List<QueueItem> queue) {
latchForQueue.countDown();
}
@Override
public void onQueueTitleChanged(CharSequence title) {
queueTitleRef.set(title);
latchForQueue.countDown();
}
@Override
public void onRepeatModeChanged(int repeatMode) {
repeatModeRef.set(repeatMode);
latchForRepeatMode.countDown();
}
@Override
public void onShuffleModeChanged(int shuffleMode) {
shuffleModeRef.set(shuffleMode);
latchForShuffleMode.countDown();
}
};
controllerCompat.registerCallback(callback, handler);
Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setPlaybackState(testState).setPlayWhenReady(testPlayWhenReady).setCurrentPosition(testCurrentPositionMs).setBufferedPosition(testBufferedPositionMs).setDuration(testDurationMs).setPlaybackParameters(playbackParameters).setTimeline(testTimeline).setPlaylistMetadata(testPlaylistMetadata).setCurrentMediaItemIndex(testItemIndex).setShuffleModeEnabled(testShuffleModeEnabled).setRepeatMode(testRepeatMode).build();
session.setPlayer(playerConfig);
assertThat(latchForPlaybackState.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(playbackStateRef.get().getBufferedPosition()).isEqualTo(testBufferedPositionMs);
assertThat(playbackStateRef.get().getPosition()).isEqualTo(testCurrentPositionMs);
assertThat(playbackStateRef.get().getPlaybackSpeed()).isWithin(EPSILON).of(playbackParameters.speed);
assertThat(latchForMetadata.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(metadataRef.get().getString(METADATA_KEY_MEDIA_ID)).isEqualTo(testMediaItems.get(testItemIndex).mediaId);
assertThat(metadataRef.get().getLong(METADATA_KEY_DURATION)).isEqualTo(testDurationMs);
@PlaybackStateCompat.State int playbackStateFromControllerCompat = MediaUtils.convertToPlaybackState(playbackStateRef.get(), metadataRef.get(), /* timeDiffMs= */
C.TIME_UNSET);
assertThat(playbackStateFromControllerCompat).isEqualTo(testState);
assertThat(metadataRef.get().getRating(METADATA_KEY_USER_RATING).hasHeart()).isTrue();
assertThat(latchForQueue.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
List<QueueItem> queue = controllerCompat.getQueue();
assertThat(queue).hasSize(testTimeline.getWindowCount());
for (int i = 0; i < testTimeline.getWindowCount(); i++) {
assertThat(queue.get(i).getDescription().getMediaId()).isEqualTo(testMediaItems.get(i).mediaId);
}
assertThat(queueTitleRef.get().toString()).isEqualTo(testPlaylistTitle);
assertThat(latchForShuffleMode.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)).isTrue();
assertThat(shuffleModeRef.get()).isEqualTo(PlaybackStateCompat.SHUFFLE_MODE_ALL);
assertThat(latchForRepeatMode.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)).isTrue();
assertThat(repeatModeRef.get()).isEqualTo(PlaybackStateCompat.REPEAT_MODE_ONE);
}
use of androidx.media3.common.Player.State 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);
}
use of androidx.media3.common.Player.State in project media by androidx.
the class MediaControllerStateMaskingWithMediaSessionCompatTest method seekTo_seekBackwardWithinSameMediaItem_resetsBufferedPosition.
@Test
public void seekTo_seekBackwardWithinSameMediaItem_resetsBufferedPosition() throws Exception {
long initialPosition = 8_000L;
long initialBufferedPosition = 9_200L;
int initialIndex = 0;
long testPosition = 7_000L;
long testBufferedPosition = testPosition;
long testTotalBufferedDuration = testBufferedPosition - testPosition;
Events testEvents = new Events(new FlagSet.Builder().addAll(EVENT_POSITION_DISCONTINUITY, EVENT_PLAYBACK_STATE_CHANGED).build());
session.setPlaybackState(new PlaybackStateCompat.Builder().setState(PlaybackStateCompat.STATE_PAUSED, initialPosition, /* playbackSpeed= */
1.0f).setBufferedPosition(initialBufferedPosition).build());
MediaController controller = controllerTestRule.createController(session.getSessionToken());
CountDownLatch latch = new CountDownLatch(3);
AtomicReference<PositionInfo> oldPositionInfoRef = new AtomicReference<>();
AtomicReference<PositionInfo> newPositionInfoRef = new AtomicReference<>();
AtomicInteger discontinuityReasonRef = new AtomicInteger();
AtomicInteger playbackStateRef = 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 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();
threadTestRule.getHandler().postAndSync(() -> {
controller.seekTo(testPosition);
currentPositionRef.set(controller.getCurrentPosition());
bufferedPositionRef.set(controller.getBufferedPosition());
totalBufferedDurationRef.set(controller.getTotalBufferedDuration());
});
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(initialIndex);
assertThat(discontinuityReasonRef.get()).isEqualTo(DISCONTINUITY_REASON_SEEK);
assertThat(playbackStateRef.get()).isEqualTo(STATE_BUFFERING);
assertThat(onEventsRef.get()).isEqualTo(testEvents);
assertThat(currentPositionRef.get()).isEqualTo(testPosition);
assertThat(bufferedPositionRef.get()).isEqualTo(testBufferedPosition);
assertThat(totalBufferedDurationRef.get()).isEqualTo(testTotalBufferedDuration);
}
Aggregations