Search in sources :

Example 1 with Rating

use of androidx.media3.common.Rating in project ExoPlayer by google.

the class SessionCallbackBuilderTest method allowedCommand_withoutPlaylist_disallowsSkipTo.

@Test
public void allowedCommand_withoutPlaylist_disallowsSkipTo() throws Exception {
    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.setMediaItem(TestUtils.createMediaItem()));
        assertPlayerResultSuccess(sessionPlayerConnector.prepare());
        CountDownLatch latch = new CountDownLatch(1);
        OnConnectedListener listener = (controller, allowedCommands) -> {
            List<Integer> disallowedCommandCodes = Arrays.asList(SessionCommand.COMMAND_CODE_PLAYER_SKIP_TO_PLAYLIST_ITEM, SessionCommand.COMMAND_CODE_PLAYER_SKIP_TO_PREVIOUS_PLAYLIST_ITEM, SessionCommand.COMMAND_CODE_PLAYER_SKIP_TO_NEXT_PLAYLIST_ITEM);
            assertDisallowedCommands(disallowedCommandCodes, allowedCommands);
            latch.countDown();
        };
        try (MediaController controller = createConnectedController(session, listener, null)) {
            assertThat(latch.await(CONTROLLER_COMMAND_WAIT_TIME_MS, MILLISECONDS)).isTrue();
            assertSessionResultFailure(controller.skipToNextPlaylistItem());
            assertSessionResultFailure(controller.skipToPreviousPlaylistItem());
            assertSessionResultFailure(controller.skipToPlaylistItem(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) MediaSession(androidx.media2.session.MediaSession) ArrayList(java.util.ArrayList) List(java.util.List) CountDownLatch(java.util.concurrent.CountDownLatch) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 2 with Rating

use of androidx.media3.common.Rating 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 3 with Rating

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

the class MediaUtils method convertToMediaMetadata.

/**
 * Creates {@link MediaMetadata} from the {@link MediaMetadataCompat} and rating type.
 */
public static MediaMetadata convertToMediaMetadata(@Nullable MediaMetadataCompat metadataCompat, @RatingCompat.Style int ratingType) {
    if (metadataCompat == null) {
        return MediaMetadata.EMPTY;
    }
    MediaMetadata.Builder builder = new MediaMetadata.Builder();
    builder.setTitle(getFirstText(metadataCompat, MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, MediaMetadataCompat.METADATA_KEY_TITLE)).setSubtitle(metadataCompat.getText(MediaMetadataCompat.METADATA_KEY_DISPLAY_SUBTITLE)).setDescription(metadataCompat.getText(MediaMetadataCompat.METADATA_KEY_DISPLAY_DESCRIPTION)).setArtist(metadataCompat.getText(MediaMetadataCompat.METADATA_KEY_ARTIST)).setAlbumTitle(metadataCompat.getText(MediaMetadataCompat.METADATA_KEY_ALBUM)).setAlbumArtist(metadataCompat.getText(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST)).setOverallRating(convertToRating(metadataCompat.getRating(MediaMetadataCompat.METADATA_KEY_RATING)));
    @Nullable Rating userRating = convertToRating(metadataCompat.getRating(MediaMetadataCompat.METADATA_KEY_USER_RATING));
    if (userRating != null) {
        builder.setUserRating(userRating);
    } else {
        builder.setUserRating(convertToRating(RatingCompat.newUnratedRating(ratingType)));
    }
    if (metadataCompat.containsKey(MediaMetadataCompat.METADATA_KEY_YEAR)) {
        long year = metadataCompat.getLong(MediaMetadataCompat.METADATA_KEY_YEAR);
        builder.setRecordingYear((int) year);
    }
    @Nullable String mediaUriString = metadataCompat.getString(MediaMetadataCompat.METADATA_KEY_MEDIA_URI);
    if (mediaUriString != null) {
        builder.setMediaUri(Uri.parse(mediaUriString));
    }
    @Nullable String artworkUriString = getFirstString(metadataCompat, MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON_URI, MediaMetadataCompat.METADATA_KEY_ALBUM_ART_URI);
    if (artworkUriString != null) {
        builder.setArtworkUri(Uri.parse(artworkUriString));
    }
    @Nullable Bitmap artworkBitmap = getFirstBitmap(metadataCompat, MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, MediaMetadataCompat.METADATA_KEY_ALBUM_ART);
    if (artworkBitmap != null) {
        try {
            byte[] artworkData = convertToByteArray(artworkBitmap);
            builder.setArtworkData(artworkData, MediaMetadata.PICTURE_TYPE_FRONT_COVER);
        } catch (IOException e) {
            Log.w(TAG, "Failed to convert artworkBitmap to artworkData", e);
        }
    }
    if (metadataCompat.containsKey(MediaMetadataCompat.METADATA_KEY_BT_FOLDER_TYPE)) {
        builder.setFolderType(convertToFolderType(metadataCompat.getLong(MediaMetadataCompat.METADATA_KEY_BT_FOLDER_TYPE)));
    } else {
        builder.setFolderType(MediaMetadata.FOLDER_TYPE_NONE);
    }
    builder.setIsPlayable(true);
    return builder.build();
}
Also used : Bitmap(android.graphics.Bitmap) HeartRating(androidx.media3.common.HeartRating) Rating(androidx.media3.common.Rating) ThumbRating(androidx.media3.common.ThumbRating) StarRating(androidx.media3.common.StarRating) PercentageRating(androidx.media3.common.PercentageRating) MediaMetadata(androidx.media3.common.MediaMetadata) IOException(java.io.IOException) Nullable(androidx.annotation.Nullable)

Example 4 with Rating

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

the class MediaSessionStub method setRatingWithMediaId.

@Override
public void setRatingWithMediaId(@Nullable IMediaController caller, int seq, String mediaId, @Nullable Bundle ratingBundle) {
    if (caller == null || ratingBundle == null) {
        return;
    }
    if (TextUtils.isEmpty(mediaId)) {
        Log.w(TAG, "setRatingWithMediaId(): Ignoring empty mediaId");
        return;
    }
    Rating rating;
    try {
        rating = Rating.CREATOR.fromBundle(ratingBundle);
    } catch (RuntimeException e) {
        Log.w(TAG, "Ignoring malformed Bundle for Rating", e);
        return;
    }
    dispatchSessionTaskWithSessionCommand(caller, seq, COMMAND_CODE_SESSION_SET_RATING, (sessionImpl, controller) -> sessionImpl.onSetRatingOnHandler(controller, mediaId, rating), MediaSessionStub::sendSessionResultWhenReady);
}
Also used : Rating(androidx.media3.common.Rating)

Example 5 with Rating

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

the class MediaSessionLegacyStub method onSetRating.

@Override
public void onSetRating(RatingCompat ratingCompat, @Nullable Bundle unusedExtras) {
    @Nullable Rating rating = MediaUtils.convertToRating(ratingCompat);
    if (rating == null) {
        Log.w(TAG, "Ignoring invalid RatingCompat " + ratingCompat);
        return;
    }
    dispatchSessionTaskWithSessionCommand(SessionCommand.COMMAND_CODE_SESSION_SET_RATING, controller -> {
        @Nullable MediaItem currentItem = sessionImpl.getPlayerWrapper().getCurrentMediaItem();
        if (currentItem == null) {
            return;
        }
        // MediaControllerCompat#setRating doesn't return a value.
        ignoreFuture(sessionImpl.onSetRatingOnHandler(controller, currentItem.mediaId, rating));
    });
}
Also used : MediaItem(androidx.media3.common.MediaItem) Rating(androidx.media3.common.Rating) Nullable(androidx.annotation.Nullable)

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