Search in sources :

Example 11 with Rating

use of androidx.media3.common.Rating 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);
        }
    }
}
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) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) MediaSession(androidx.media2.session.MediaSession) UriMediaItem(androidx.media2.common.UriMediaItem) MediaItem(androidx.media2.common.MediaItem) ArrayList(java.util.ArrayList) List(java.util.List) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 12 with Rating

use of androidx.media3.common.Rating in project media by androidx.

the class MediaSessionCompatCallbackWithMediaControllerTest method setRatingWithoutMediaId.

@Test
public void setRatingWithoutMediaId() throws Exception {
    float ratingValue = 3.5f;
    Rating rating = new StarRating(5, ratingValue);
    RemoteMediaController controller = createControllerAndWaitConnection();
    sessionCallback.reset(1);
    controller.setRating(rating);
    assertThat(sessionCallback.await(TIMEOUT_MS)).isTrue();
    assertThat(sessionCallback.onSetRatingCalled).isTrue();
    assertThat(MediaUtils.convertToRating(sessionCallback.rating)).isEqualTo(rating);
}
Also used : Rating(androidx.media3.common.Rating) StarRating(androidx.media3.common.StarRating) StarRating(androidx.media3.common.StarRating) SmallTest(androidx.test.filters.SmallTest) Test(org.junit.Test)

Example 13 with Rating

use of androidx.media3.common.Rating in project media by androidx.

the class MediaSessionPermissionTest method setRating.

@Test
public void setRating() throws Exception {
    String mediaId = "testSetRating";
    Rating rating = new StarRating(5, 3.5f);
    createSessionWithAvailableCommands(createSessionCommandsWith(new SessionCommand(COMMAND_CODE_SESSION_SET_RATING)), Player.Commands.EMPTY);
    controllerTestRule.createRemoteController(session.getToken()).setRating(mediaId, rating);
    assertThat(callback.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(callback.onSetRatingCalled).isTrue();
    assertThat(callback.mediaId).isEqualTo(mediaId);
    assertThat(callback.rating).isEqualTo(rating);
    createSessionWithAvailableCommands(createSessionCommandsWith(new SessionCommand(COMMAND_CODE_SESSION_SET_MEDIA_URI)), Player.Commands.EMPTY);
    controllerTestRule.createRemoteController(session.getToken()).setRating(mediaId, rating);
    assertThat(callback.countDownLatch.await(NO_RESPONSE_TIMEOUT_MS, MILLISECONDS)).isFalse();
    assertThat(callback.onSetRatingCalled).isFalse();
}
Also used : Rating(androidx.media3.common.Rating) StarRating(androidx.media3.common.StarRating) StarRating(androidx.media3.common.StarRating) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 14 with Rating

use of androidx.media3.common.Rating in project media by androidx.

the class MediaUtilsTest method assertRatingEquals.

void assertRatingEquals(Rating rating, RatingCompat ratingCompat) {
    if (rating == null && ratingCompat == null) {
        return;
    }
    assertThat(rating.isRated()).isEqualTo(ratingCompat.isRated());
    if (rating instanceof HeartRating) {
        assertThat(ratingCompat.getRatingStyle()).isEqualTo(RatingCompat.RATING_HEART);
        assertThat(((HeartRating) rating).isHeart()).isEqualTo(ratingCompat.hasHeart());
    } else if (rating instanceof ThumbRating) {
        assertThat(ratingCompat.getRatingStyle()).isEqualTo(RatingCompat.RATING_THUMB_UP_DOWN);
        assertThat(((ThumbRating) rating).isThumbsUp()).isEqualTo(ratingCompat.isThumbUp());
    } else if (rating instanceof StarRating) {
        StarRating starRating = (StarRating) rating;
        switch(starRating.getMaxStars()) {
            case 3:
                assertThat(ratingCompat.getRatingStyle()).isEqualTo(RatingCompat.RATING_3_STARS);
                break;
            case 4:
                assertThat(ratingCompat.getRatingStyle()).isEqualTo(RatingCompat.RATING_4_STARS);
                break;
            case 5:
                assertThat(ratingCompat.getRatingStyle()).isEqualTo(RatingCompat.RATING_5_STARS);
                break;
            // fall out
            default:
        }
        assertThat(starRating.getStarRating()).isEqualTo(ratingCompat.getStarRating());
    } else if (rating instanceof PercentageRating) {
        assertThat(ratingCompat.getRatingStyle()).isEqualTo(RatingCompat.RATING_PERCENTAGE);
        assertThat(((PercentageRating) rating).getPercent()).isEqualTo(ratingCompat.getPercentRating());
    }
}
Also used : ThumbRating(androidx.media3.common.ThumbRating) PercentageRating(androidx.media3.common.PercentageRating) StarRating(androidx.media3.common.StarRating) HeartRating(androidx.media3.common.HeartRating)

Example 15 with Rating

use of androidx.media3.common.Rating in project media by androidx.

the class MediaSessionCallbackWithMediaControllerCompatTest method setRating.

@Test
public void setRating() throws Exception {
    int ratingType = RatingCompat.RATING_5_STARS;
    float ratingValue = 3.5f;
    RatingCompat rating = RatingCompat.newStarRating(ratingType, ratingValue);
    String mediaId = "media_id";
    CountDownLatch latch = new CountDownLatch(1);
    SessionCallback callback = new TestSessionCallback() {

        @Override
        public ListenableFuture<SessionResult> onSetRating(MediaSession session, ControllerInfo controller, String mediaIdOut, Rating ratingOut) {
            assertThat(mediaIdOut).isEqualTo(mediaId);
            assertThat(ratingOut).isEqualTo(MediaUtils.convertToRating(rating));
            latch.countDown();
            return Futures.immediateFuture(new SessionResult(RESULT_SUCCESS));
        }
    };
    handler.postAndSync(() -> {
        List<MediaItem> mediaItems = MediaTestUtils.createMediaItems(mediaId);
        player.timeline = MediaTestUtils.createTimeline(mediaItems);
    });
    session = new MediaSession.Builder(context, player).setId("setRating").setSessionCallback(callback).build();
    controller = new RemoteMediaControllerCompat(context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */
    true);
    controller.getTransportControls().setRating(rating);
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
}
Also used : Rating(androidx.media3.common.Rating) SessionCallback(androidx.media3.session.MediaSession.SessionCallback) CountDownLatch(java.util.concurrent.CountDownLatch) RatingCompat(android.support.v4.media.RatingCompat) MediaItem(androidx.media3.common.MediaItem) ControllerInfo(androidx.media3.session.MediaSession.ControllerInfo) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Aggregations

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