Search in sources :

Example 1 with MediaStyle

use of androidx.media3.session.MediaStyleNotificationHelper.MediaStyle in project media by androidx.

the class PlayerNotificationManager method createNotification.

/**
 * Creates the notification given the current session state.
 *
 * @param builder The builder used to build the last notification, or {@code null}. Re-using the
 *     builder when possible can prevent notification flicker when {@code Util#SDK_INT} < 21.
 * @param ongoing Whether the notification should be ongoing.
 * @param largeIcon The large icon to be used.
 * @return The {@link NotificationCompat.Builder} on which to call {@link
 *     NotificationCompat.Builder#build()} to obtain the notification, or {@code null} if no
 *     notification should be displayed.
 */
@Nullable
protected NotificationCompat.Builder createNotification(@Nullable NotificationCompat.Builder builder, boolean ongoing, @Nullable Bitmap largeIcon) {
    Player player = session.getPlayer();
    if (player.getPlaybackState() == Player.STATE_IDLE && player.getCurrentTimeline().isEmpty()) {
        return null;
    }
    if (builder == null) {
        builder = new NotificationCompat.Builder(context, channelId);
    }
    List<CommandButton> actionButtons = getActionButtons();
    for (int i = 0; i < actionButtons.size(); i++) {
        CommandButton button = actionButtons.get(i);
        NotificationCompat.Action action = new NotificationCompat.Action(button.iconResId, button.displayName, createBroadcastIntent(context, button, instanceId));
        builder.addAction(action);
    }
    MediaStyle mediaStyle = new MediaStyle(session);
    mediaStyle.setShowActionsInCompactView(getActionButtonIndicesForCompactView(actionButtons));
    // Configure dismiss action prior to API 21 ('x' button).
    mediaStyle.setShowCancelButton(!ongoing);
    mediaStyle.setCancelButtonIntent(dismissPendingIntent);
    builder.setStyle(mediaStyle);
    // Set intent which is sent if the user selects 'clear all'
    builder.setDeleteIntent(dismissPendingIntent);
    // Set notification properties from getters.
    builder.setBadgeIconType(badgeIconType).setOngoing(ongoing).setColor(color).setColorized(colorized).setSmallIcon(smallIconResourceId).setVisibility(visibility).setPriority(priority).setDefaults(defaults);
    // Changing "showWhen" causes notification flicker if SDK_INT < 21.
    if (Util.SDK_INT >= 21 && useChronometer && player.isPlaying() && !player.isPlayingAd() && !player.isCurrentWindowDynamic() && player.getPlaybackParameters().speed == 1f) {
        builder.setWhen(System.currentTimeMillis() - player.getContentPosition()).setShowWhen(true).setUsesChronometer(true);
    } else {
        builder.setShowWhen(false).setUsesChronometer(false);
    }
    // Set media specific notification properties from MediaDescriptionAdapter.
    builder.setContentTitle(mediaDescriptionAdapter.getCurrentContentTitle(session));
    builder.setContentText(mediaDescriptionAdapter.getCurrentContentText(session));
    builder.setSubText(mediaDescriptionAdapter.getCurrentSubText(session));
    if (largeIcon == null) {
        largeIcon = mediaDescriptionAdapter.getCurrentLargeIcon(session, new BitmapCallback(++currentNotificationTag));
    }
    setLargeIcon(builder, largeIcon);
    MediaController controller = getMediaControllerOrNull();
    if (controller != null) {
        builder.setContentIntent(controller.getSessionActivity());
    }
    if (groupKey != null) {
        builder.setGroup(groupKey);
    }
    builder.setOnlyAlertOnce(true);
    return builder;
}
Also used : Player(androidx.media3.common.Player) MediaStyle(androidx.media3.session.MediaStyleNotificationHelper.MediaStyle) NotificationCompat(androidx.core.app.NotificationCompat) Nullable(androidx.annotation.Nullable)

Example 2 with MediaStyle

use of androidx.media3.session.MediaStyleNotificationHelper.MediaStyle in project media by androidx.

the class DefaultMediaNotificationProvider method createNotification.

@Override
public MediaNotification createNotification(MediaController mediaController, MediaNotification.ActionFactory actionFactory, Callback onNotificationChangedCallback) {
    ensureNotificationChannel();
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
    // TODO(b/193193926): Filter actions depending on the player's available commands.
    // Skip to previous action.
    builder.addAction(actionFactory.createMediaAction(IconCompat.createWithResource(context, R.drawable.media3_notification_seek_to_previous), context.getString(R.string.media3_controls_seek_to_previous_description), MediaNotification.ActionFactory.COMMAND_SKIP_TO_PREVIOUS));
    if (mediaController.getPlaybackState() == Player.STATE_ENDED || !mediaController.getPlayWhenReady()) {
        // Play action.
        builder.addAction(actionFactory.createMediaAction(IconCompat.createWithResource(context, R.drawable.media3_notification_play), context.getString(R.string.media3_controls_play_description), MediaNotification.ActionFactory.COMMAND_PLAY));
    } else {
        // Pause action.
        builder.addAction(actionFactory.createMediaAction(IconCompat.createWithResource(context, R.drawable.media3_notification_pause), context.getString(R.string.media3_controls_pause_description), MediaNotification.ActionFactory.COMMAND_PAUSE));
    }
    // Skip to next action.
    builder.addAction(actionFactory.createMediaAction(IconCompat.createWithResource(context, R.drawable.media3_notification_seek_to_next), context.getString(R.string.media3_controls_seek_to_next_description), MediaNotification.ActionFactory.COMMAND_SKIP_TO_NEXT));
    // Set metadata info in the notification.
    MediaMetadata metadata = mediaController.getMediaMetadata();
    builder.setContentTitle(metadata.title).setContentText(metadata.artist);
    if (metadata.artworkData != null) {
        Bitmap artworkBitmap = BitmapFactory.decodeByteArray(metadata.artworkData, 0, metadata.artworkData.length);
        builder.setLargeIcon(artworkBitmap);
    }
    androidx.media.app.NotificationCompat.MediaStyle mediaStyle = new androidx.media.app.NotificationCompat.MediaStyle().setCancelButtonIntent(actionFactory.createMediaActionPendingIntent(MediaNotification.ActionFactory.COMMAND_STOP)).setShowActionsInCompactView(1);
    Notification notification = builder.setContentIntent(mediaController.getSessionActivity()).setDeleteIntent(actionFactory.createMediaActionPendingIntent(MediaNotification.ActionFactory.COMMAND_STOP)).setOnlyAlertOnce(true).setSmallIcon(getSmallIconResId(context)).setStyle(mediaStyle).setVisibility(NotificationCompat.VISIBILITY_PUBLIC).setOngoing(false).build();
    return new MediaNotification(NOTIFICATION_ID, notification);
}
Also used : Bitmap(android.graphics.Bitmap) NotificationCompat(androidx.core.app.NotificationCompat) MediaMetadata(androidx.media3.common.MediaMetadata) Notification(android.app.Notification)

Aggregations

NotificationCompat (androidx.core.app.NotificationCompat)2 Notification (android.app.Notification)1 Bitmap (android.graphics.Bitmap)1 Nullable (androidx.annotation.Nullable)1 MediaMetadata (androidx.media3.common.MediaMetadata)1 Player (androidx.media3.common.Player)1 MediaStyle (androidx.media3.session.MediaStyleNotificationHelper.MediaStyle)1