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));
}
}
}
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);
}
}
}
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();
}
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);
}
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));
});
}
Aggregations