Hadi Ahmadi
Hadi Ahmadi

Reputation: 1992

how to change player notification title and large icon in Media3 in android 11+?

I using MediaSessionService from Media3 to play audio in background. I update player notification using setMediaNotificationProvider. but in android 11(API 30) and up, the title, content text, and large icon are not changing and showing default things related to audio file. is this a bug or am I missing some things?

val mediaNotificationProvider = object : MediaNotification.Provider{
    override fun createNotification(
        mediaSession: MediaSession,
        customLayout: ImmutableList<CommandButton>,
        actionFactory: MediaNotification.ActionFactory,
        onNotificationChangedCallback: MediaNotification.Provider.Callback
    ): MediaNotification {
        createMediaNotification(mediaSession)
        return MediaNotification(NOTIFICATION_ID, notificationBuilder.build())
    }

    override fun handleCustomCommand(
        session: MediaSession,
        action: String,
        extras: Bundle
    ): Boolean {
        return false
    }
}

private lateinit var notificationBuilder: NotificationCompat.Builder

fun  createMediaNotification(
    session: MediaSession,
) {
    notificationBuilder = NotificationCompat.Builder(context,
        NOTIFICATION_CHANNEL_ID
    )
        .setSmallIcon(R.drawable.notification)
        .setContentTitle("test title")
        .setContentText("test text")
        .setLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.notification_large_icon))
        .setStyle(MediaStyleNotificationHelper.MediaStyle(session))

}

Upvotes: 0

Views: 987

Answers (1)

Hadi Ahmadi
Hadi Ahmadi

Reputation: 1992

My code was correct and the problem was only a bug in Media3. by downgrading media3 from 1.0.1 to 1.0.0-beta03 my code works now!

Upvotes: 0

Related Questions