use of androidx.media2.session.MediaController in project ExoPlayer by google.
the class SessionCallbackBuilderTest method setPostConnectCallback_afterConnect_receivesOnPostConnect.
@Test
public void setPostConnectCallback_afterConnect_receivesOnPostConnect() throws Exception {
CountDownLatch postConnectLatch = new CountDownLatch(1);
SessionCallbackBuilder.PostConnectCallback postConnectCallback = (session, controllerInfo) -> postConnectLatch.countDown();
try (MediaSession session = createMediaSession(sessionPlayerConnector, new SessionCallbackBuilder(context, sessionPlayerConnector).setPostConnectCallback(postConnectCallback).build())) {
try (MediaController controller = createConnectedController(session)) {
assertThat(postConnectLatch.await(CONTROLLER_COMMAND_WAIT_TIME_MS, MILLISECONDS)).isTrue();
}
}
}
use of androidx.media2.session.MediaController in project ExoPlayer by google.
the class SessionCallbackBuilderTest method setCustomCommandProvider_withCustomCommandProvider_receivesCustomCommand.
@Test
public void setCustomCommandProvider_withCustomCommandProvider_receivesCustomCommand() throws Exception {
SessionCommand testCommand = new SessionCommand("exo.ext.media2.COMMAND", null);
CountDownLatch latch = new CountDownLatch(1);
SessionCallbackBuilder.CustomCommandProvider provider = new SessionCallbackBuilder.CustomCommandProvider() {
@Override
public SessionResult onCustomCommand(MediaSession session, MediaSession.ControllerInfo controllerInfo, SessionCommand customCommand, @Nullable Bundle args) {
assertThat(customCommand.getCustomAction()).isEqualTo(testCommand.getCustomAction());
assertThat(args).isNull();
latch.countDown();
return new SessionResult(SessionResult.RESULT_SUCCESS, null);
}
@Override
public SessionCommandGroup getCustomCommands(MediaSession session, MediaSession.ControllerInfo controllerInfo) {
return new SessionCommandGroup.Builder().addCommand(testCommand).build();
}
};
try (MediaSession session = createMediaSession(sessionPlayerConnector, new SessionCallbackBuilder(context, sessionPlayerConnector).setCustomCommandProvider(provider).build())) {
OnAllowedCommandsChangedListener listener = (controller, allowedCommands) -> {
boolean foundCustomCommand = false;
for (SessionCommand command : allowedCommands.getCommands()) {
if (TextUtils.equals(testCommand.getCustomAction(), command.getCustomAction())) {
foundCustomCommand = true;
break;
}
}
assertThat(foundCustomCommand).isTrue();
};
try (MediaController controller = createConnectedController(session, null, listener)) {
assertSessionResultSuccess(controller.sendCustomCommand(testCommand, null), CONTROLLER_COMMAND_WAIT_TIME_MS);
assertThat(latch.await(0, MILLISECONDS)).isTrue();
}
}
}
use of androidx.media2.session.MediaController in project ExoPlayer by google.
the class SessionCallbackBuilderTest method setRatingCallback_withRatingCallback_receivesRatingCallback.
@Test
public void setRatingCallback_withRatingCallback_receivesRatingCallback() throws Exception {
String testMediaId = "testRating";
Rating testRating = new HeartRating(true);
CountDownLatch latch = new CountDownLatch(1);
SessionCallbackBuilder.RatingCallback ratingCallback = (session, controller, mediaId, rating) -> {
assertThat(mediaId).isEqualTo(testMediaId);
assertThat(rating).isEqualTo(testRating);
latch.countDown();
return SessionResult.RESULT_SUCCESS;
};
try (MediaSession session = createMediaSession(sessionPlayerConnector, new SessionCallbackBuilder(context, sessionPlayerConnector).setRatingCallback(ratingCallback).build())) {
try (MediaController controller = createConnectedController(session)) {
assertSessionResultSuccess(controller.setRating(testMediaId, testRating), CONTROLLER_COMMAND_WAIT_TIME_MS);
assertThat(latch.await(0, MILLISECONDS)).isTrue();
}
}
}
use of androidx.media2.session.MediaController in project ExoPlayer by google.
the class SessionCallbackBuilderTest method allowedCommand_whenPlaylistSet_allowsSkipTo.
@Test
public void allowedCommand_whenPlaylistSet_allowsSkipTo() throws Exception {
List<MediaItem> testPlaylist = new ArrayList<>();
testPlaylist.add(TestUtils.createMediaItem(R.raw.video_desks));
testPlaylist.add(TestUtils.createMediaItem(R.raw.video_not_seekable));
int testRewindIncrementMs = 100;
int testFastForwardIncrementMs = 100;
try (MediaSession session = createMediaSession(sessionPlayerConnector, new SessionCallbackBuilder(context, sessionPlayerConnector).setRatingCallback((mediaSession, controller, mediaId, rating) -> SessionResult.RESULT_ERROR_BAD_VALUE).setRewindIncrementMs(testRewindIncrementMs).setFastForwardIncrementMs(testFastForwardIncrementMs).setMediaItemProvider(new SessionCallbackBuilder.MediaIdMediaItemProvider()).build())) {
assertPlayerResultSuccess(sessionPlayerConnector.setPlaylist(testPlaylist, null));
assertPlayerResultSuccess(sessionPlayerConnector.prepare());
OnConnectedListener connectedListener = (controller, allowedCommands) -> {
List<Integer> allowedCommandCodes = Arrays.asList(SessionCommand.COMMAND_CODE_PLAYER_SKIP_TO_NEXT_PLAYLIST_ITEM, SessionCommand.COMMAND_CODE_PLAYER_SEEK_TO, SessionCommand.COMMAND_CODE_SESSION_REWIND, SessionCommand.COMMAND_CODE_SESSION_FAST_FORWARD);
assertAllowedCommands(allowedCommandCodes, allowedCommands);
List<Integer> disallowedCommandCodes = Arrays.asList(SessionCommand.COMMAND_CODE_PLAYER_SKIP_TO_PREVIOUS_PLAYLIST_ITEM);
assertDisallowedCommands(disallowedCommandCodes, allowedCommands);
};
CountDownLatch allowedCommandChangedLatch = new CountDownLatch(1);
OnAllowedCommandsChangedListener allowedCommandChangedListener = (controller, allowedCommands) -> {
List<Integer> allowedCommandCodes = Arrays.asList(SessionCommand.COMMAND_CODE_PLAYER_SKIP_TO_PREVIOUS_PLAYLIST_ITEM);
assertAllowedCommands(allowedCommandCodes, allowedCommands);
List<Integer> disallowedCommandCodes = Arrays.asList(SessionCommand.COMMAND_CODE_PLAYER_SKIP_TO_NEXT_PLAYLIST_ITEM, SessionCommand.COMMAND_CODE_PLAYER_SEEK_TO, SessionCommand.COMMAND_CODE_SESSION_REWIND, SessionCommand.COMMAND_CODE_SESSION_FAST_FORWARD);
assertDisallowedCommands(disallowedCommandCodes, allowedCommands);
allowedCommandChangedLatch.countDown();
};
try (MediaController controller = createConnectedController(session, connectedListener, allowedCommandChangedListener)) {
assertPlayerResultSuccess(sessionPlayerConnector.skipToNextPlaylistItem());
assertThat(allowedCommandChangedLatch.await(CONTROLLER_COMMAND_WAIT_TIME_MS, MILLISECONDS)).isTrue();
// Also test whether the rewind fails as expected.
assertSessionResultFailure(controller.rewind());
assertThat(sessionPlayerConnector.getCurrentPosition()).isEqualTo(0);
assertThat(controller.getCurrentPosition()).isEqualTo(0);
}
}
}
Aggregations