Search in sources :

Example 11 with MediaSession

use of androidx.media2.session.MediaSession 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;
}
Also used : MediaController(androidx.media2.session.MediaController) SessionCommandGroup(androidx.media2.session.SessionCommandGroup) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 12 with MediaSession

use of androidx.media2.session.MediaSession 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();
        }
    }
}
Also used : Context(android.content.Context) HeartRating(androidx.media2.session.HeartRating) Arrays(java.util.Arrays) Bundle(android.os.Bundle) Uri(android.net.Uri) MediaSession(androidx.media2.session.MediaSession) RunWith(org.junit.runner.RunWith) SessionResult(androidx.media2.session.SessionResult) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) TestUtils.assertPlayerResultSuccess(com.google.android.exoplayer2.ext.media2.TestUtils.assertPlayerResultSuccess) ApplicationProvider(androidx.test.core.app.ApplicationProvider) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) SessionCommand(androidx.media2.session.SessionCommand) ContextCompat(androidx.core.content.ContextCompat) LargeTest(androidx.test.filters.LargeTest) Before(org.junit.Before) SessionPlayer(androidx.media2.common.SessionPlayer) MediaMetadata(androidx.media2.common.MediaMetadata) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) Executor(java.util.concurrent.Executor) TextUtils(android.text.TextUtils) UriMediaItem(androidx.media2.common.UriMediaItem) Test(org.junit.Test) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Truth.assertThat(com.google.common.truth.Truth.assertThat) MediaItem(androidx.media2.common.MediaItem) Rating(androidx.media2.common.Rating) R(com.google.android.exoplayer2.ext.media2.test.R) CountDownLatch(java.util.concurrent.CountDownLatch) SessionCommandGroup(androidx.media2.session.SessionCommandGroup) List(java.util.List) Nullable(androidx.annotation.Nullable) Rule(org.junit.Rule) MediaController(androidx.media2.session.MediaController) RawResourceDataSource(com.google.android.exoplayer2.upstream.RawResourceDataSource) MediaController(androidx.media2.session.MediaController) SessionPlayer(androidx.media2.common.SessionPlayer) CountDownLatch(java.util.concurrent.CountDownLatch) Uri(android.net.Uri) MediaSession(androidx.media2.session.MediaSession) UriMediaItem(androidx.media2.common.UriMediaItem) MediaItem(androidx.media2.common.MediaItem) MediaMetadata(androidx.media2.common.MediaMetadata) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 13 with MediaSession

use of androidx.media2.session.MediaSession 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();
        }
    }
}
Also used : Context(android.content.Context) HeartRating(androidx.media2.session.HeartRating) Arrays(java.util.Arrays) Bundle(android.os.Bundle) Uri(android.net.Uri) MediaSession(androidx.media2.session.MediaSession) RunWith(org.junit.runner.RunWith) SessionResult(androidx.media2.session.SessionResult) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) TestUtils.assertPlayerResultSuccess(com.google.android.exoplayer2.ext.media2.TestUtils.assertPlayerResultSuccess) ApplicationProvider(androidx.test.core.app.ApplicationProvider) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) SessionCommand(androidx.media2.session.SessionCommand) ContextCompat(androidx.core.content.ContextCompat) LargeTest(androidx.test.filters.LargeTest) Before(org.junit.Before) SessionPlayer(androidx.media2.common.SessionPlayer) MediaMetadata(androidx.media2.common.MediaMetadata) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) Executor(java.util.concurrent.Executor) TextUtils(android.text.TextUtils) UriMediaItem(androidx.media2.common.UriMediaItem) Test(org.junit.Test) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Truth.assertThat(com.google.common.truth.Truth.assertThat) MediaItem(androidx.media2.common.MediaItem) Rating(androidx.media2.common.Rating) R(com.google.android.exoplayer2.ext.media2.test.R) CountDownLatch(java.util.concurrent.CountDownLatch) SessionCommandGroup(androidx.media2.session.SessionCommandGroup) List(java.util.List) Nullable(androidx.annotation.Nullable) Rule(org.junit.Rule) MediaController(androidx.media2.session.MediaController) RawResourceDataSource(com.google.android.exoplayer2.upstream.RawResourceDataSource) MediaController(androidx.media2.session.MediaController) MediaSession(androidx.media2.session.MediaSession) CountDownLatch(java.util.concurrent.CountDownLatch) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 14 with MediaSession

use of androidx.media2.session.MediaSession 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();
        }
    }
}
Also used : Context(android.content.Context) HeartRating(androidx.media2.session.HeartRating) Arrays(java.util.Arrays) Bundle(android.os.Bundle) Uri(android.net.Uri) MediaSession(androidx.media2.session.MediaSession) RunWith(org.junit.runner.RunWith) SessionResult(androidx.media2.session.SessionResult) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) TestUtils.assertPlayerResultSuccess(com.google.android.exoplayer2.ext.media2.TestUtils.assertPlayerResultSuccess) ApplicationProvider(androidx.test.core.app.ApplicationProvider) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) SessionCommand(androidx.media2.session.SessionCommand) ContextCompat(androidx.core.content.ContextCompat) LargeTest(androidx.test.filters.LargeTest) Before(org.junit.Before) SessionPlayer(androidx.media2.common.SessionPlayer) MediaMetadata(androidx.media2.common.MediaMetadata) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) Executor(java.util.concurrent.Executor) TextUtils(android.text.TextUtils) UriMediaItem(androidx.media2.common.UriMediaItem) Test(org.junit.Test) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Truth.assertThat(com.google.common.truth.Truth.assertThat) MediaItem(androidx.media2.common.MediaItem) Rating(androidx.media2.common.Rating) R(com.google.android.exoplayer2.ext.media2.test.R) CountDownLatch(java.util.concurrent.CountDownLatch) SessionCommandGroup(androidx.media2.session.SessionCommandGroup) List(java.util.List) Nullable(androidx.annotation.Nullable) Rule(org.junit.Rule) MediaController(androidx.media2.session.MediaController) RawResourceDataSource(com.google.android.exoplayer2.upstream.RawResourceDataSource) MediaController(androidx.media2.session.MediaController) Bundle(android.os.Bundle) CountDownLatch(java.util.concurrent.CountDownLatch) SessionResult(androidx.media2.session.SessionResult) MediaSession(androidx.media2.session.MediaSession) SessionCommand(androidx.media2.session.SessionCommand) Nullable(androidx.annotation.Nullable) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 15 with MediaSession

use of androidx.media2.session.MediaSession 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();
        }
    }
}
Also used : Context(android.content.Context) HeartRating(androidx.media2.session.HeartRating) Arrays(java.util.Arrays) Bundle(android.os.Bundle) Uri(android.net.Uri) MediaSession(androidx.media2.session.MediaSession) RunWith(org.junit.runner.RunWith) SessionResult(androidx.media2.session.SessionResult) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) TestUtils.assertPlayerResultSuccess(com.google.android.exoplayer2.ext.media2.TestUtils.assertPlayerResultSuccess) ApplicationProvider(androidx.test.core.app.ApplicationProvider) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) SessionCommand(androidx.media2.session.SessionCommand) ContextCompat(androidx.core.content.ContextCompat) LargeTest(androidx.test.filters.LargeTest) Before(org.junit.Before) SessionPlayer(androidx.media2.common.SessionPlayer) MediaMetadata(androidx.media2.common.MediaMetadata) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) Executor(java.util.concurrent.Executor) TextUtils(android.text.TextUtils) UriMediaItem(androidx.media2.common.UriMediaItem) Test(org.junit.Test) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Truth.assertThat(com.google.common.truth.Truth.assertThat) MediaItem(androidx.media2.common.MediaItem) Rating(androidx.media2.common.Rating) R(com.google.android.exoplayer2.ext.media2.test.R) CountDownLatch(java.util.concurrent.CountDownLatch) SessionCommandGroup(androidx.media2.session.SessionCommandGroup) List(java.util.List) Nullable(androidx.annotation.Nullable) Rule(org.junit.Rule) MediaController(androidx.media2.session.MediaController) RawResourceDataSource(com.google.android.exoplayer2.upstream.RawResourceDataSource) MediaController(androidx.media2.session.MediaController) MediaSession(androidx.media2.session.MediaSession) HeartRating(androidx.media2.session.HeartRating) Rating(androidx.media2.common.Rating) CountDownLatch(java.util.concurrent.CountDownLatch) HeartRating(androidx.media2.session.HeartRating) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Aggregations

MediaController (androidx.media2.session.MediaController)14 MediaSession (androidx.media2.session.MediaSession)13 LargeTest (androidx.test.filters.LargeTest)13 Test (org.junit.Test)13 SessionCommandGroup (androidx.media2.session.SessionCommandGroup)12 CountDownLatch (java.util.concurrent.CountDownLatch)12 Nullable (androidx.annotation.Nullable)11 MediaItem (androidx.media2.common.MediaItem)10 SessionCommand (androidx.media2.session.SessionCommand)10 Context (android.content.Context)9 Uri (android.net.Uri)9 Bundle (android.os.Bundle)9 TextUtils (android.text.TextUtils)9 ContextCompat (androidx.core.content.ContextCompat)9 MediaMetadata (androidx.media2.common.MediaMetadata)9 Rating (androidx.media2.common.Rating)9 SessionPlayer (androidx.media2.common.SessionPlayer)9 UriMediaItem (androidx.media2.common.UriMediaItem)9 HeartRating (androidx.media2.session.HeartRating)9 SessionResult (androidx.media2.session.SessionResult)9