use of androidx.media3.common.Player.Events in project media by androidx.
the class MediaControllerStateMaskingTest method setDeviceMuted.
@Test
public void setDeviceMuted() throws Exception {
boolean testDeviceMuted = true;
Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setDeviceMuted(false).build();
remoteSession.setPlayer(playerConfig);
MediaController controller = controllerTestRule.createController(remoteSession.getToken());
CountDownLatch latch = new CountDownLatch(2);
AtomicBoolean deviceMutedFromCallbackRef = new AtomicBoolean();
Player.Listener listener = new Player.Listener() {
@Override
public void onDeviceVolumeChanged(int volume, boolean muted) {
deviceMutedFromCallbackRef.set(muted);
latch.countDown();
}
@Override
public void onEvents(Player player, Player.Events events) {
latch.countDown();
}
};
threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
AtomicBoolean deviceMutedFromGetterRef = new AtomicBoolean();
threadTestRule.getHandler().postAndSync(() -> {
controller.setDeviceMuted(testDeviceMuted);
deviceMutedFromGetterRef.set(controller.isDeviceMuted());
});
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(deviceMutedFromCallbackRef.get()).isEqualTo(testDeviceMuted);
assertThat(deviceMutedFromGetterRef.get()).isEqualTo(testDeviceMuted);
}
use of androidx.media3.common.Player.Events in project media by androidx.
the class MediaControllerStateMaskingTest method setVolume.
@Test
public void setVolume() throws Exception {
float testVolume = 0.5f;
Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setVolume(0).build();
remoteSession.setPlayer(playerConfig);
MediaController controller = controllerTestRule.createController(remoteSession.getToken());
CountDownLatch latch = new CountDownLatch(2);
AtomicReference<Float> volumeFromCallbackRef = new AtomicReference<>();
Player.Listener listener = new Player.Listener() {
@Override
public void onVolumeChanged(float volume) {
volumeFromCallbackRef.set(volume);
latch.countDown();
}
@Override
public void onEvents(Player player, Player.Events events) {
latch.countDown();
}
};
threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
AtomicReference<Float> volumeFromGetterRef = new AtomicReference<>();
threadTestRule.getHandler().postAndSync(() -> {
controller.setVolume(testVolume);
volumeFromGetterRef.set(controller.getVolume());
});
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(volumeFromCallbackRef.get()).isEqualTo(testVolume);
assertThat(volumeFromGetterRef.get()).isEqualTo(testVolume);
}
use of androidx.media3.common.Player.Events in project media by androidx.
the class MediaControllerStateMaskingTest method stop.
@Test
public void stop() throws Exception {
long duration = 6000L;
long initialCurrentPosition = 3000L;
long initialBufferedPosition = duration;
int initialBufferedPercentage = 100;
long initialTotalBufferedDuration = initialBufferedPosition - initialCurrentPosition;
int testPlaybackState = Player.STATE_IDLE;
long testCurrentPosition = 3000L;
long testBufferedPosition = testCurrentPosition;
int testBufferedPercentage = 50;
long testTotalBufferedDuration = testBufferedPosition - testCurrentPosition;
Timeline testTimeline = MediaTestUtils.createTimeline(3);
PlaybackException testPlaybackException = new PlaybackException(/* message= */
"test", /* cause= */
null, PlaybackException.ERROR_CODE_REMOTE_ERROR);
Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setDuration(duration).setPlaybackState(Player.STATE_READY).setPlayerError(testPlaybackException).setTimeline(testTimeline).setCurrentPosition(initialCurrentPosition).setBufferedPosition(initialBufferedPosition).setBufferedPercentage(initialBufferedPercentage).setTotalBufferedDuration(initialTotalBufferedDuration).build();
remoteSession.setPlayer(playerConfig);
MediaController controller = controllerTestRule.createController(remoteSession.getToken());
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<PlaybackException> playerErrorFromGetterRef = new AtomicReference<>();
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());
playerErrorFromGetterRef.set(controller.getPlayerError());
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().contains(Player.EVENT_PLAYBACK_STATE_CHANGED)).isTrue();
assertThat(playbackStateFromGetterRef.get()).isEqualTo(testPlaybackState);
assertThat(playerErrorFromGetterRef.get().errorInfoEquals(testPlaybackException)).isTrue();
assertThat(timelineFromGetterRef.get().getWindowCount()).isEqualTo(testTimeline.getWindowCount());
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.Events in project media by androidx.
the class MediaControllerStateMaskingTest method prepare.
@Test
public void prepare() throws Exception {
int testPlaybackState = Player.STATE_ENDED;
Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setPlaybackState(Player.STATE_IDLE).setPlayerError(new PlaybackException(/* message= */
"test", /* cause= */
null, PlaybackException.ERROR_CODE_REMOTE_ERROR)).build();
remoteSession.setPlayer(playerConfig);
MediaController controller = controllerTestRule.createController(remoteSession.getToken());
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<PlaybackException> playerErrorRef = new AtomicReference<>();
threadTestRule.getHandler().postAndSync(() -> {
controller.prepare();
playbackStateFromGetterRef.set(controller.getPlaybackState());
playerErrorRef.set(controller.getPlayerError());
});
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(playbackStateFromCallbackRef.get()).isEqualTo(testPlaybackState);
assertThat(onEventsRef.get().contains(Player.EVENT_PLAYBACK_STATE_CHANGED)).isTrue();
assertThat(playbackStateFromGetterRef.get()).isEqualTo(testPlaybackState);
assertThat(playerErrorRef.get()).isNull();
}
use of androidx.media3.common.Player.Events in project media by androidx.
the class MediaControllerStateMaskingTest method assertAddMediaItems.
private void assertAddMediaItems(int initialMediaItemCount, int initialMediaItemIndex, int initialPeriodIndex, Timeline initialTimeline, int testMediaItemCount, int testIndex, int testCurrentMediaItemIndex, int testNextMediaItemIndex, int testPreviousMediaItemIndex, int testCurrentPeriodIndex, int testCurrentWindowFirstPeriodIndex, int testCurrentWindowLastPeriodIndex) throws Exception {
Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setTimeline(initialTimeline).setCurrentMediaItemIndex(initialMediaItemIndex).setCurrentPeriodIndex(initialPeriodIndex).build();
remoteSession.setPlayer(playerConfig);
MediaController controller = controllerTestRule.createController(remoteSession.getToken());
CountDownLatch latch = new CountDownLatch(2);
AtomicReference<Timeline> newTimelineRef = new AtomicReference<>();
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 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();
AtomicInteger currentPeriodIndexRef = new AtomicInteger();
threadTestRule.getHandler().postAndSync(() -> {
controller.addMediaItems(/* index= */
testIndex, createMediaItems(testMediaItemCount));
currentMediaItemIndexRef.set(controller.getCurrentMediaItemIndex());
nextMediaItemIndexRef.set(controller.getNextMediaItemIndex());
previousMediaItemIndexRef.set(controller.getPreviousMediaItemIndex());
currentPeriodIndexRef.set(controller.getCurrentPeriodIndex());
});
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertTimeline(newTimelineRef.get(), initialMediaItemCount + testMediaItemCount, testCurrentMediaItemIndex, /* testFirstPeriodIndex= */
testCurrentWindowFirstPeriodIndex, /* testLastPeriodIndex= */
testCurrentWindowLastPeriodIndex);
assertThat(onEventsRef.get().contains(Player.EVENT_TIMELINE_CHANGED)).isTrue();
assertThat(currentMediaItemIndexRef.get()).isEqualTo(testCurrentMediaItemIndex);
assertThat(nextMediaItemIndexRef.get()).isEqualTo(testNextMediaItemIndex);
assertThat(previousMediaItemIndexRef.get()).isEqualTo(testPreviousMediaItemIndex);
assertThat(currentPeriodIndexRef.get()).isEqualTo(testCurrentPeriodIndex);
}
Aggregations