Repository navigation
Description
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.
How can i achieve this?

Activity
tianyif commentedon Jan 8, 2024
Hi @AmrSubZero,
You can utilize the
replaceMediaItemto achieve this.In this way, the player can emit an
EVENT_MEDIA_METADATA_CHANGEDevent, and media3 sessionMediaNotificationManageror legacyPlayerNotificationManagercan update the notification when receive the metadata changed event.Hope this helps!
AmrSubZero commentedon Jan 10, 2024
Hello @tianyif
Actually the
player.replaceMediaItem()did workupdating the title, but i think it does not suit my case, here's whyThe logic, since Media3 or ExoPlayer does not have built in
Positiontracker/listener (as far as i know), so i built my own using (Runnable/Handler) runs every 1 second.See here
Then i check if the current position equals something, there i need to update the meta title like "Part: 13", this runs very frequently!
Using
player.replaceMediaItem()kinda resets the current position of the player for the second it did a replace, then it gets back to normal position.See here
Therefore, when i get incorrect
current positionthings go wrong.also is it a best practice calling
player.replaceMediaItem()for a time like every 5 seconds? i can think it will break someway somehow!So, my hopes:
titlewithout needing to replace the current media item.player.replaceMediaItem()Thanks!
tianyif commentedon Jan 10, 2024
@AmrSubZero,
Thanks for the details!
Did you reproduce this on demo-session app? I tried to reproduce on demo app with pasting your snippet in
DemoPlaybackServiceand adding some simple logs to check the positions. The positions I got keep increasing. My metadata update frequency was even higher (after I got the current position per 1 sec, I replaced the media item with a new title).I'm not sure if you're suggesting something like a current position listener. We had such requests (eg. google/ExoPlayer#3980) before, and the short answer is sending a huge amount of callbacks according to the position update frequency (per 10 ms) is less efficient than polling the progress infrequently. And the
ProgressTrackershould be sufficient for tracking the playback position for most purposes.AmrSubZero commentedon Jan 11, 2024
Hello @tianyif thanks for keeping up!
after hours of debugging and trying several methods, here's what i've discovered!
Exoplayer playerinside theMediaSessionServiceMediaController playerinMainActivitywhich i receive as documentedWhen i tested getting
current positionfrom both (with different methods) as we discussed, with doingplayer.replaceMediaItem()frequently (every 1 second) :MediaSessionService
ExoPlayer playeralways giving the correctcurrent positionand it never gets interrupted by callingplayer.replaceMediaItem()no matter if clicking pause and play still correct.MainActivity
MediaController playersomehow gets interrupted by callingplayer.replaceMediaItem()and giving incorrect (position reset) right after calling the .replaceMediaItem (happens if i pause and continue playing again)i tried several methods to track
current positionhere's by getting it directly fromPlayerViewSee here
Either way, updaing the MediaMetaData title (frequently) is working now by calling
.replaceMediaItem()thanks to your support.But i'm still curious, why the
current poisitiongets interrupted (when pausing and playing) fromMediaController playerinMainActivityand it does not fromExoPlayer playerinMediaSessionServiceHere is a repo of projects for both ways.
Thanks! 👏
tianyif commentedon Jan 12, 2024
Hi @AmrSubZero,
Thanks for the impressive details! Yes, I could reproduce the interrupted position with the test controller app with connecting to a session from the demo session app.
The reason being this is based on the four facts -
MediaItemis replaced at theMediaController, thePlayerInfo(including the position info) got masked by at the controller side, with the default position of the new item (default is 0).mediaMetadatachange, after theMediaItemis replaced at the session player.SessionPositionInfoto the controller (default interval is 3s), and controller will then update itsplayerInfo.SessionPositionInfo.getCurrentPosition()at the controller side, the returned current position is interpolated by the latestplayerInfo.SessionPositionInfo.positionInfo.positionMsand the elapsed time from the last update.Then I added some logs and found that -
The wrong position was marked as WRONG, which is 1003. I marked the logs corresponding to each above steps, and you can see between replacing the item and getting the wrong position 1003, there is neither [2] and [3] that update the
SessionPositionInfo. The elapsed time since the last update is 1003, so plus masked position which is 0, we wrongly got 1003.My rough thought is around whether we should use the default position 0 to mask the current position when the item is being replaced (corresponding to the above [1]). It makes sense in the case that the current playing item is completely removed and the default position of the next item will be immediately used, but in the case that the current playing item is replaced, using the current position instead of 0 would be a better option.
We will further look into and provide a proper solution to it. Thanks a lot for all the details and repo of the project!
Avoid position jumping back when controller replaces the current item
Avoid position jumping back when controller replaces the current item