I'm using Media3 ExoPlayer@1.2.0 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:
I can't seem to find any documentation or workaround to achieve this approach, don't know either it's possible or not.
How can i achieve this?
I managed to update the MediaMetaData title
during playback by calling player.replaceMediaItem(..)
as mentioned here
MediaItem oldItem = player.getCurrentMediaItem();
if(oldItem != null) {
MediaItem newItem = oldItem
.buildUpon()
.setMediaMetadata(player.getMediaMetadata()
.buildUpon()
.setTitle("New Title")
.build())
.build();
player.replaceMediaItem(player.getCurrentMediaItemIndex(), newItem);
}
In this way, the player can emit an EVENT_MEDIA_METADATA_CHANGED event, and media3 session MediaNotificationManager or legacy PlayerNotificationManager can update the notification when receive the metadata changed event.