0

enter image description here

MediaSessionCompat mediaSessionCompat = new MediaSessionCompat(ctx, "tag");

When I set .style(), notification is not output. Is there anything in the mediaSessionCompat that you need to do other than setting context and tag?

Elikill58
  • 4,050
  • 24
  • 23
  • 45
Juro
  • 73
  • 1
  • 7
  • What's not working? Does the notification display? Do you use setMetaData on mediasession? Update your question to provide more info. – private static Dec 06 '21 at 09:30
  • Thanks. When i set .style() notification is not displaying How can i use setMetaData on mediasession? – Juro Dec 07 '21 at 00:06

1 Answers1

2

If you use the setStyle method on a notification, you need to provide the MediaSession with Metadata.

private void updateMetadata () {
mediaSession.setMetadata(new MediaMetadataCompat.Builder()
                                     .putString(MediaMetadataCompat.METADATA_KEY_ARTIST, song.getArtistTitle())
                                     .putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, song.getArtistTitle())
                                     .putString(MediaMetadataCompat.METADATA_KEY_ALBUM, song.getAlbumTitle())
                                     .putString(MediaMetadataCompat.METADATA_KEY_TITLE, song.getTitle())
                                     .putLong(MediaMetadataCompat.METADATA_KEY_DURATION, song.getDuration())
                                     .putLong(MediaMetadataCompat.METADATA_KEY_TRACK_NUMBER, getPosition())
                                     .putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, null)
                                     .build());
}

Don't forget to call this method, for example when your data changes. I don't know how you handle that so I can't provide a correct place for it. I'd say the same place you update the notification when a song changes and it's only a suggestion.

private static
  • 745
  • 8
  • 17