Search in sources :

Example 6 with MediaSession

use of androidx.media2.session.MediaSession in project ExoPlayer by google.

the class SessionCallbackBuilderTest method allowedCommand_afterCurrentMediaItemPrepared_notifiesSeekToAvailable.

@Test
public void allowedCommand_afterCurrentMediaItemPrepared_notifiesSeekToAvailable() throws Exception {
    List<MediaItem> testPlaylist = new ArrayList<>();
    testPlaylist.add(TestUtils.createMediaItem(R.raw.video_desks));
    UriMediaItem secondPlaylistItem = TestUtils.createMediaItem(R.raw.video_big_buck_bunny);
    testPlaylist.add(secondPlaylistItem);
    CountDownLatch readAllowedLatch = new CountDownLatch(1);
    playerTestRule.setDataSourceInstrumentation(dataSpec -> {
        if (dataSpec.uri.equals(secondPlaylistItem.getUri())) {
            try {
                assertThat(readAllowedLatch.await(PLAYER_STATE_CHANGE_WAIT_TIME_MS, MILLISECONDS)).isTrue();
            } catch (Exception e) {
                assertWithMessage("Unexpected exception %s", e).fail();
            }
        }
    });
    try (MediaSession session = createMediaSession(sessionPlayerConnector, new SessionCallbackBuilder(context, sessionPlayerConnector).build())) {
        assertPlayerResultSuccess(sessionPlayerConnector.setPlaylist(testPlaylist, null));
        assertPlayerResultSuccess(sessionPlayerConnector.prepare());
        CountDownLatch seekToAllowedForSecondMediaItem = new CountDownLatch(1);
        OnAllowedCommandsChangedListener allowedCommandsChangedListener = (controller, allowedCommands) -> {
            if (allowedCommands.hasCommand(SessionCommand.COMMAND_CODE_PLAYER_SEEK_TO) && controller.getCurrentMediaItemIndex() == 1) {
                seekToAllowedForSecondMediaItem.countDown();
            }
        };
        try (MediaController controller = createConnectedController(session, /* onConnectedListener= */
        null, allowedCommandsChangedListener)) {
            assertPlayerResultSuccess(sessionPlayerConnector.skipToNextPlaylistItem());
            readAllowedLatch.countDown();
            assertThat(seekToAllowedForSecondMediaItem.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) UriMediaItem(androidx.media2.common.UriMediaItem) MediaItem(androidx.media2.common.MediaItem) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) UriMediaItem(androidx.media2.common.UriMediaItem) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 7 with MediaSession

use of androidx.media2.session.MediaSession in project ExoPlayer by google.

the class SessionCallbackBuilderTest method constructor.

@Test
public void constructor() throws Exception {
    try (MediaSession session = createMediaSession(sessionPlayerConnector, new SessionCallbackBuilder(context, sessionPlayerConnector).build())) {
        assertPlayerResultSuccess(sessionPlayerConnector.setMediaItem(TestUtils.createMediaItem()));
        assertPlayerResultSuccess(sessionPlayerConnector.prepare());
        OnConnectedListener listener = (controller, allowedCommands) -> {
            List<Integer> disallowedCommandCodes = Arrays.asList(// no rating callback
            SessionCommand.COMMAND_CODE_SESSION_SET_RATING, // no media item provider
            SessionCommand.COMMAND_CODE_PLAYER_ADD_PLAYLIST_ITEM, SessionCommand.COMMAND_CODE_PLAYER_REPLACE_PLAYLIST_ITEM, // no media item provider
            SessionCommand.COMMAND_CODE_PLAYER_SET_MEDIA_ITEM, // no media item provider
            SessionCommand.COMMAND_CODE_PLAYER_SET_PLAYLIST, // no current media item
            SessionCommand.COMMAND_CODE_SESSION_REWIND, // no current media item
            SessionCommand.COMMAND_CODE_SESSION_FAST_FORWARD);
            assertDisallowedCommands(disallowedCommandCodes, allowedCommands);
        };
        try (MediaController controller = createConnectedController(session, listener, null)) {
            assertThat(controller.getPlayerState()).isEqualTo(SessionPlayer.PLAYER_STATE_PAUSED);
        }
    }
}
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) ArrayList(java.util.ArrayList) List(java.util.List) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 8 with MediaSession

use of androidx.media2.session.MediaSession 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);
        }
    }
}
Also used : MediaController(androidx.media2.session.MediaController) MediaSession(androidx.media2.session.MediaSession) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test) LargeTest(androidx.test.filters.LargeTest)

Example 9 with MediaSession

use of androidx.media2.session.MediaSession 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);
        }
    }
}
Also used : MediaController(androidx.media2.session.MediaController) MediaSession(androidx.media2.session.MediaSession) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test) LargeTest(androidx.test.filters.LargeTest)

Example 10 with MediaSession

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