I build a web radio player with Media3 1.0.0-beta03. I use the sample code from Developers page.
It's generated a media notification automatically but I don't know how to add Title and sub title to this.
Here is my media service:
class PlaybackService : MediaSessionService(), MediaSession.Callback {
private object LC {
lateinit var exoPlayer: ExoPlayer
lateinit var mediaSession: MediaSession
}
override fun onCreate() {
super.onCreate()
log("----------------------------- MediaSessionService, onCreate")
LC.exoPlayer = ExoPlayer.Builder(this).build()
LC.exoPlayer.addListener(ExoListener())
LC.exoPlayer.setAudioAttributes(AudioAttributes.Builder().setContentType(AUDIO_CONTENT_TYPE_MUSIC).setUsage(USAGE_MEDIA).build(),true)
LC.mediaSession = MediaSession.Builder(this, LC.exoPlayer).setCallback(this).build()
}
override fun onGetSession(controllerInfo: MediaSession.ControllerInfo): MediaSession = LC.mediaSession
override fun onAddMediaItems(mediaSession: MediaSession, controller: MediaSession.ControllerInfo, mediaItems: MutableList<MediaItem>): ListenableFuture<MutableList<MediaItem>> {
val updatedMediaItems = mediaItems.map { it.buildUpon().setUri(it.mediaId).build() }.toMutableList()
return Futures.immediateFuture(updatedMediaItems)
}
override fun onDestroy() {
log("----------------------------- MediaSessionService, onDestroy")
LC.exoPlayer.stop()
LC.exoPlayer.release()
LC.mediaSession.release()
super.onDestroy()
exitProcess(0)
}
}
I tryed the onUpdateNotification
Update :
There is also another way, which I found out does the Job better.
In onCreate() function of MediaSessionService, We can set a MediaNotificationProvider like so.
private lateinit var nBuilder: NotificationCompat.Builder
override fun onCreate(){
super.onCreate()
// init notificationCompat.Builder before setting the MediaNotificationProvider
this.setMediaNotificationProvider(object : MediaNotification.Provider{
override fun createNotification(
mediaSession: MediaSession,// this is the session we pass to style
customLayout: ImmutableList<CommandButton>,
actionFactory: MediaNotification.ActionFactory,
onNotificationChangedCallback: MediaNotification.Provider.Callback
): MediaNotification {
createNotification(mediaSession)
// notification should be created before you return here
return MediaNotification(NOTIFICATION_ID,nBuilder.build())
}
override fun handleCustomCommand(
session: MediaSession,
action: String,
extras: Bundle
): Boolean {
TODO("Not yet implemented")
}
})
}
fun createNotification(session: MediaSession) {
val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(NotificationChannel(notification_id,"Channel", NotificationManager.IMPORTANCE_LOW))
// NotificationCompat.Builder here.
nBuilder = NotificationCompat.Builder(this,notification_id)
// Text can be set here
// but I believe setting MediaMetaData to MediaSession would be enough.
// I havent tested it deeply yet but did display artist from session
.setSmallIcon(R.drawable.your_drawable)
.setContentTitle("your Content title")
.setContentText("your content text")
// set session here
.setStyle(MediaStyleNotificationHelper.MediaStyle(session))
// we don build.
}
and finally if you want to update notification info yourself
you can do so by calling a function like this..
private fun updateNotification(/*parameter*/){
nBuilder.setContentTitle("text")
nBuilder.setContentText("subtext")
nManager.notify(NOTIFICATION_ID,nBuilder.build())
}