use of androidx.media2.session.MediaController in project ExoPlayer by google.
the class SessionCallbackBuilderTest method setFastForwardIncrementMs_withPositiveFastForwardIncrement_fastsForward.
@LargeTest
@Test
public void setFastForwardIncrementMs_withPositiveFastForwardIncrement_fastsForward() throws Exception {
int testResId = R.raw.video_big_buck_bunny;
int testDuration = 10_000;
int tolerance = 100;
int testSeekPosition = 2_000;
int testFastForwardIncrementMs = 300;
TestUtils.loadResource(testResId, sessionPlayerConnector);
// seekTo() sometimes takes couple of seconds. Disable default timeout behavior.
try (MediaSession session = createMediaSession(sessionPlayerConnector, new SessionCallbackBuilder(context, sessionPlayerConnector).setFastForwardIncrementMs(testFastForwardIncrementMs).setSeekTimeoutMs(0).build())) {
try (MediaController controller = createConnectedController(session)) {
// Prepare first to ensure that seek() works.
assertSessionResultSuccess(controller.prepare(), PLAYER_STATE_CHANGE_OVER_SESSION_WAIT_TIME_MS);
assertThat((float) sessionPlayerConnector.getDuration()).isWithin(tolerance).of(testDuration);
assertSessionResultSuccess(controller.seekTo(testSeekPosition), PLAYER_STATE_CHANGE_OVER_SESSION_WAIT_TIME_MS);
assertThat((float) sessionPlayerConnector.getCurrentPosition()).isWithin(tolerance).of(testSeekPosition);
// Test fast-forward
assertSessionResultSuccess(controller.fastForward(), PLAYER_STATE_CHANGE_OVER_SESSION_WAIT_TIME_MS);
assertThat((float) sessionPlayerConnector.getCurrentPosition()).isWithin(tolerance).of(testSeekPosition + testFastForwardIncrementMs);
}
}
}
use of androidx.media2.session.MediaController in project ExoPlayer by google.
the class SessionCallbackBuilderTest method setRewindIncrementMs_withPositiveRewindIncrement_rewinds.
@LargeTest
@Test
public void setRewindIncrementMs_withPositiveRewindIncrement_rewinds() throws Exception {
int testResId = R.raw.video_big_buck_bunny;
int testDuration = 10_000;
int tolerance = 100;
int testSeekPosition = 2_000;
int testRewindIncrementMs = 500;
TestUtils.loadResource(testResId, sessionPlayerConnector);
// seekTo() sometimes takes couple of seconds. Disable default timeout behavior.
try (MediaSession session = createMediaSession(sessionPlayerConnector, new SessionCallbackBuilder(context, sessionPlayerConnector).setRewindIncrementMs(testRewindIncrementMs).setSeekTimeoutMs(0).build())) {
try (MediaController controller = createConnectedController(session)) {
// Prepare first to ensure that seek() works.
assertSessionResultSuccess(controller.prepare(), PLAYER_STATE_CHANGE_OVER_SESSION_WAIT_TIME_MS);
assertThat((float) sessionPlayerConnector.getDuration()).isWithin(tolerance).of(testDuration);
assertSessionResultSuccess(controller.seekTo(testSeekPosition), PLAYER_STATE_CHANGE_OVER_SESSION_WAIT_TIME_MS);
assertThat((float) sessionPlayerConnector.getCurrentPosition()).isWithin(tolerance).of(testSeekPosition);
// Test rewind
assertSessionResultSuccess(controller.rewind(), PLAYER_STATE_CHANGE_OVER_SESSION_WAIT_TIME_MS);
assertThat((float) sessionPlayerConnector.getCurrentPosition()).isWithin(tolerance).of(testSeekPosition - testRewindIncrementMs);
}
}
}
use of androidx.media2.session.MediaController in project ExoPlayer by google.
the class SessionCallbackBuilderTest method setSkipCallback_withSkipForward_receivesOnSkipForward.
@Test
public void setSkipCallback_withSkipForward_receivesOnSkipForward() throws Exception {
CountDownLatch skipForwardCalledLatch = new CountDownLatch(1);
SessionCallbackBuilder.SkipCallback skipCallback = new SessionCallbackBuilder.SkipCallback() {
@Override
public int onSkipBackward(MediaSession session, MediaSession.ControllerInfo controllerInfo) {
return SessionResult.RESULT_ERROR_NOT_SUPPORTED;
}
@Override
public int onSkipForward(MediaSession session, MediaSession.ControllerInfo controllerInfo) {
skipForwardCalledLatch.countDown();
return SessionResult.RESULT_SUCCESS;
}
};
try (MediaSession session = createMediaSession(sessionPlayerConnector, new SessionCallbackBuilder(context, sessionPlayerConnector).setSkipCallback(skipCallback).build())) {
try (MediaController controller = createConnectedController(session)) {
assertSessionResultSuccess(controller.skipForward(), CONTROLLER_COMMAND_WAIT_TIME_MS);
assertThat(skipForwardCalledLatch.await(0, MILLISECONDS)).isTrue();
}
}
}
use of androidx.media2.session.MediaController in project ExoPlayer by google.
the class SessionCallbackBuilderTest method createConnectedController.
private MediaController createConnectedController(MediaSession session, OnConnectedListener onConnectedListener, OnAllowedCommandsChangedListener onAllowedCommandsChangedListener) throws Exception {
CountDownLatch latch = new CountDownLatch(1);
MediaController.ControllerCallback callback = new MediaController.ControllerCallback() {
@Override
public void onAllowedCommandsChanged(MediaController controller, SessionCommandGroup commands) {
if (onAllowedCommandsChangedListener != null) {
onAllowedCommandsChangedListener.onAllowedCommandsChanged(controller, commands);
}
}
@Override
public void onConnected(MediaController controller, SessionCommandGroup allowedCommands) {
if (onConnectedListener != null) {
onConnectedListener.onConnected(controller, allowedCommands);
}
latch.countDown();
}
};
MediaController controller = new MediaController.Builder(context).setSessionToken(session.getToken()).setControllerCallback(ContextCompat.getMainExecutor(context), callback).build();
latch.await();
return controller;
}
use of androidx.media2.session.MediaController in project ExoPlayer by google.
the class SessionCallbackBuilderTest method setMediaItemProvider_withMediaItemProvider_receivesOnCreateMediaItem.
@Test
public void setMediaItemProvider_withMediaItemProvider_receivesOnCreateMediaItem() throws Exception {
Uri testMediaUri = RawResourceDataSource.buildRawResourceUri(R.raw.audio);
CountDownLatch providerLatch = new CountDownLatch(1);
SessionCallbackBuilder.MediaIdMediaItemProvider mediaIdMediaItemProvider = new SessionCallbackBuilder.MediaIdMediaItemProvider();
SessionCallbackBuilder.MediaItemProvider provider = (session, controllerInfo, mediaId) -> {
assertThat(mediaId).isEqualTo(testMediaUri.toString());
providerLatch.countDown();
return mediaIdMediaItemProvider.onCreateMediaItem(session, controllerInfo, mediaId);
};
CountDownLatch currentMediaItemChangedLatch = new CountDownLatch(1);
sessionPlayerConnector.registerPlayerCallback(executor, new SessionPlayer.PlayerCallback() {
@Override
public void onCurrentMediaItemChanged(SessionPlayer player, MediaItem item) {
MediaMetadata metadata = item.getMetadata();
assertThat(metadata.getString(MediaMetadata.METADATA_KEY_MEDIA_ID)).isEqualTo(testMediaUri.toString());
currentMediaItemChangedLatch.countDown();
}
});
try (MediaSession session = createMediaSession(sessionPlayerConnector, new SessionCallbackBuilder(context, sessionPlayerConnector).setMediaItemProvider(provider).build())) {
try (MediaController controller = createConnectedController(session)) {
assertSessionResultSuccess(controller.setMediaItem(testMediaUri.toString()), PLAYER_STATE_CHANGE_OVER_SESSION_WAIT_TIME_MS);
assertThat(providerLatch.await(0, MILLISECONDS)).isTrue();
assertThat(currentMediaItemChangedLatch.await(CONTROLLER_COMMAND_WAIT_TIME_MS, MILLISECONDS)).isTrue();
}
}
}
Aggregations