媒体会话在 AOD 上有“无标题”(始终显示)

Yun*_*LIU 7 notifications android lockscreen exoplayer

在我的应用程序中,我显示了一个带有前台服务的通知,该服务负责播放音乐。通知由 com.google.android.exoplayer2.ui.PlayerNotificationManager android.support.v4.media.session.MediaSessionCompat com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector

    mediaSession = MediaSessionCompat(this, "Player", null, null)
    mediaSession.isActive = true
    mediaSessionConnector = MediaSessionConnector(mediaSession)
    mediaSessionConnector.setPlayer(exoPlayer)
    playerNotificationManager = PlayerNotificationManager.createWithNotificationChannel(
            this,
            "notification_channel_player",
            R.string.notification_channel_name_player,
            0,
            PLAYER_NOTIFICATION_ID,
            object : PlayerNotificationManager.MediaDescriptionAdapter {
                override fun createCurrentContentIntent(player: Player?): PendingIntent? {
                    // intent
                }

                override fun getCurrentLargeIcon(player: Player?, callback: PlayerNotificationManager.BitmapCallback?): Bitmap? {
                    // large icon
                }

                override fun getCurrentContentText(player: Player?): String? {
                    // artist
                }

                override fun getCurrentContentTitle(player: Player?): String {
                    // title
                }

            },
            object : NotificationListener {
                override fun onNotificationPosted(notificationId: Int, notification: Notification?, ongoing: Boolean) {
                    startForeground(notificationId, notification)
                }
            })

    playerNotificationManager.setSmallIcon(R.drawable.ic_notification)
    // has previous and next
    playerNotificationManager.setUseNavigationActions(true)
    playerNotificationManager.setUseNavigationActionsInCompactView(true)
    // no fast-forward and rewind
    playerNotificationManager.setFastForwardIncrementMs(0)
    playerNotificationManager.setRewindIncrementMs(0)
    // no stop
    playerNotificationManager.setUseStopAction(false)

    playerNotificationManager.setMediaSessionToken(mediaSession.sessionToken)
    playerNotificationManager.setPlayer(exoPlayer)
Run Code Online (Sandbox Code Playgroud)

当屏幕打开时,显示内容标题和文本没有问题。但是当我锁定屏幕并处于 AOD 模式时,在我的 Pixel 3 上我看到显示“无标题”。但是如果我使用 Apple Music,它会很好地显示标题和艺术家。

我的应用程序: 在此处输入图片说明

苹果音乐: 在此处输入图片说明

我的问题是,如何根据我当前的实现配置此标题和文本?谢谢。

Yun*_*LIU 6

我只是回答我自己的问题,因为我已经找到并解决了这个问题。

我只设置了通知的媒体描述适配器,但实际上,媒体会话也需要设置元数据。由于我们使用的是mediaSessionConnector,因此可以通过将 a 传递给QueueNavigator来设置它mediaSessionConnector,因此我们可以使用播放器实例和窗口索引来构建当前媒体的元数据。前任:

    val timelineQueueNavigator = object : TimelineQueueNavigator(mediaSession) {
        override fun getMediaDescription(player: Player?, windowIndex: Int): MediaDescriptionCompat {
            player?.let { safePlayer ->
                return MediaDescriptionCompat.Builder().apply {
                    setTitle("......")
                    setSubtitle("......")
                }.build()
            }
            return MediaDescriptionCompat.Builder().build()
        }
    }
    mediaSessionConnector.setQueueNavigator(timelineQueueNavigator)
Run Code Online (Sandbox Code Playgroud)

另一点是,默认情况下mediaSessionConnector使用MediaSessionConnector.DefaultMediaMetadataProvider. 它没有设置METADATA_KEY_ARTIST哪个将在 AOD 模式下作为艺术家使用。所以我创建了自己的 MediaMetadataProvider,添加了METADATA_KEY_ARTIST.

if (description.subtitle != null) {
    val subTitle = description.subtitle.toString()
    builder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, subTitle)
    builder.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_SUBTITLE, subTitle)
}
Run Code Online (Sandbox Code Playgroud)