use of androidx.media3.session.MediaSession.SessionCallback in project media by androidx.
the class MediaSessionCallbackWithMediaControllerCompatTest method onDisconnected_afterTimeout_isCalled.
@Test
public void onDisconnected_afterTimeout_isCalled() throws Exception {
CountDownLatch disconnectedLatch = new CountDownLatch(1);
session = new MediaSession.Builder(context, player).setId("onDisconnected_afterTimeout_isCalled").setSessionCallback(new SessionCallback() {
private ControllerInfo connectedController;
@Override
public MediaSession.ConnectionResult onConnect(MediaSession session, ControllerInfo controller) {
if (EXPECTED_CONTROLLER_PACKAGE_NAME.equals(controller.getPackageName())) {
connectedController = controller;
return SessionCallback.super.onConnect(session, controller);
}
return MediaSession.ConnectionResult.reject();
}
@Override
public void onDisconnected(MediaSession session, ControllerInfo controller) {
if (Util.areEqual(connectedController, controller)) {
disconnectedLatch.countDown();
}
}
}).build();
// Make onDisconnected() to be called immediately after the connection.
session.setLegacyControllerConnectionTimeoutMs(0);
controller = new RemoteMediaControllerCompat(context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */
true);
// Invoke any command for session to recognize the controller compat.
controller.getTransportControls().seekTo(111);
assertThat(disconnectedLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
}
use of androidx.media3.session.MediaSession.SessionCallback in project media by androidx.
the class MediaSessionCallbackWithMediaControllerCompatTest method prepareFromSearch.
@Test
public void prepareFromSearch() throws Exception {
String query = "test_query";
Bundle bundle = new Bundle();
bundle.putString("key", "value");
CountDownLatch latch = new CountDownLatch(1);
SessionCallback callback = new TestSessionCallback() {
@Override
public int onSetMediaUri(MediaSession session, ControllerInfo controller, Uri uri, Bundle extras) {
assertThat(uri.toString()).isEqualTo("androidx://media3-session/prepareFromSearch?query=" + query);
assertThat(TestUtils.equals(bundle, extras)).isTrue();
latch.countDown();
return RESULT_SUCCESS;
}
};
session = new MediaSession.Builder(context, player).setId("prepareFromSearch").setSessionCallback(callback).build();
controller = new RemoteMediaControllerCompat(context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */
true);
controller.getTransportControls().prepareFromSearch(query, bundle);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_PREPARE, TIMEOUT_MS);
}
use of androidx.media3.session.MediaSession.SessionCallback in project media by androidx.
the class MediaSessionCallbackWithMediaControllerCompatTest method prepareFromMediaUri.
@Test
public void prepareFromMediaUri() throws Exception {
Uri mediaUri = Uri.parse("foo://bar");
Bundle bundle = new Bundle();
bundle.putString("key", "value");
CountDownLatch latch = new CountDownLatch(1);
SessionCallback callback = new TestSessionCallback() {
@Override
public int onSetMediaUri(MediaSession session, ControllerInfo controller, Uri uri, Bundle extras) {
assertThat(uri).isEqualTo(mediaUri);
assertThat(TestUtils.equals(bundle, extras)).isTrue();
latch.countDown();
return RESULT_SUCCESS;
}
};
session = new MediaSession.Builder(context, player).setId("prepareFromMediaUri").setSessionCallback(callback).build();
controller = new RemoteMediaControllerCompat(context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */
true);
controller.getTransportControls().prepareFromUri(mediaUri, bundle);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_PREPARE, TIMEOUT_MS);
}
use of androidx.media3.session.MediaSession.SessionCallback in project media by androidx.
the class MediaSessionCallbackWithMediaControllerCompatTest method onCommandRequest.
@Test
public void onCommandRequest() throws Exception {
ArrayList<Integer> commands = new ArrayList<>();
CountDownLatch latchForPause = new CountDownLatch(1);
SessionCallback callback = new TestSessionCallback() {
@Override
public int onPlayerCommandRequest(MediaSession session, ControllerInfo controllerInfo, @Player.Command int command) {
assertThat(controllerInfo.isTrusted()).isFalse();
commands.add(command);
if (command == COMMAND_PLAY_PAUSE) {
latchForPause.countDown();
return RESULT_ERROR_INVALID_STATE;
}
return RESULT_SUCCESS;
}
};
session = new MediaSession.Builder(context, player).setId("onPlayerCommandRequest").setSessionCallback(callback).build();
controller = new RemoteMediaControllerCompat(context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */
true);
controller.getTransportControls().pause();
assertThat(latchForPause.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
Thread.sleep(NO_RESPONSE_TIMEOUT_MS);
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_PAUSE)).isFalse();
assertThat(commands).hasSize(1);
assertThat(commands.get(0)).isEqualTo(COMMAND_PLAY_PAUSE);
controller.getTransportControls().prepare();
player.awaitMethodCalled(MockPlayer.METHOD_PREPARE, TIMEOUT_MS);
assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_PAUSE)).isFalse();
assertThat(commands).hasSize(2);
assertThat(commands.get(1)).isEqualTo(COMMAND_PREPARE);
}
use of androidx.media3.session.MediaSession.SessionCallback in project media by androidx.
the class MediaSessionCallbackWithMediaControllerCompatTest method playFromMediaUri.
@Test
public void playFromMediaUri() throws Exception {
Uri request = Uri.parse("foo://bar");
Bundle bundle = new Bundle();
bundle.putString("key", "value");
CountDownLatch latch = new CountDownLatch(1);
SessionCallback callback = new TestSessionCallback() {
@Override
public int onSetMediaUri(MediaSession session, ControllerInfo controller, Uri uri, Bundle extras) {
assertThat(uri).isEqualTo(request);
assertThat(TestUtils.equals(bundle, extras)).isTrue();
latch.countDown();
return RESULT_SUCCESS;
}
};
session = new MediaSession.Builder(context, player).setId("playFromMediaUri").setSessionCallback(callback).build();
controller = new RemoteMediaControllerCompat(context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */
true);
controller.getTransportControls().playFromUri(request, bundle);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
player.awaitMethodCalled(MockPlayer.METHOD_PLAY, TIMEOUT_MS);
}
Aggregations