Skip to content

Update Notification Title during playback #951

Closed
@AmrSubZero

Description

@AmrSubZero

Using Media3 [email protected] with a PlaybackService that extends MediaSessionService

using setMediaNotificationProvider to create a Notification for API below 33.

public class PlaybackService extends MediaSessionService {

    public void onCreate() {

        // Initialize ExoPlayer
        ExoPlayer player = new ExoPlayer.Builder(this, ...).build();

        setMediaNotificationProvider(new MediaNotification.Provider() {
            @NonNull
            @Override
            public MediaNotification createNotification(@NonNull MediaSession mediaSession, @NonNull ImmutableList<CommandButton> customLayout, @NonNull MediaNotification.ActionFactory actionFactory, @NonNull Callback onNotificationChangedCallback) {

                createMediaNotification(mediaSession);

                return new MediaNotification(PLAYBACK_NOTIFICATION_ID, notificationBuilder.build());
            }

            // ...
        });

        // ...

    });

    public void createMediaNotification(MediaSession mediaSession) {

        MediaMetadata metadata = mediaSession.getPlayer().getMediaMetadata();

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(PlaybackService.this, PLAYBACK_CHANNEL_ID)
            // ...
            .setContentTitle(metadata.title)
            .setContentText(metadata.description)
            .setStyle(new MediaStyleNotificationHelper.MediaStyle(mediaSession));

    }

}

in MainActivity i set the MediaMetaData (title/description)

List<MediaItem> mediaItems = new ArrayList<>();

MediaItem mediaItem = new MediaItem.Builder()
    .setMediaId(ITEM_ID)
    .setMediaMetadata(new MediaMetadata.Builder()
            .setTitle(ITEM_TITLE)
            .setDescription(ITEM_DESCRIPTION)
            // ..
    .setUri(ITEM_URI)
.build();

mediaItems.add(mediaItem);

player.setMediaItems(mediaItems);

player.prepare();
player.play();

TLDR: i need to update title/description of the media playback notification (during playback), after it's already set before using .setMediaMetadata(new MediaMetadata.Builder().setTitle(..)) and .setContentTitle(metadata.title)

Now, what i'm trying to achieve, lets say at some point (during playback) i need to update the title/description (or MediaMetaData title/description) of the Notification for the current playing media item, wether it is the Media3 Session Notification or Legacy Notification below API Level 33

// a method in MainActivity
private void onPlayerProgressChanged(long current_playback_time) {

    if(current_playback_time >= something) {
        
        // Here need to update the notification title/description
        // Like .setDescription("Something else")
    }

}

Here's how it's currently working:

Notifications

I can't seem to find any documentation or workaround to achieve this approach.

How can i achieve this?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions