android - 媒体 session 在 AOD 上有 "no title"(始终显示)

标签 android notifications exoplayer lockscreen

在我的应用程序中,我显示了一个带有前台服务的通知,该服务负责播放音乐。通知由处理 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)

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

我的应用: enter image description here

苹果音乐: enter image description here

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

最佳答案

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

我只设置了通知的媒体描述适配器,但实际上媒体 session 也需要设置元数据。 由于我们使用的是 mediaSessionConnector,因此可以通过将 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)

还有一点是,默认情况下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)
}

关于android - 媒体 session 在 AOD 上有 "no title"(始终显示),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59144878/

相关文章:

android - 应用关闭时收到通知

github - 在exo player android中播放加密视频?

android - setCurrentTab 不工作

php - 菜单栏通知徽章更新问题

objective-c - 启用/禁用特定的 UILocalNotification 并一次设置多个通知

android - 错误 :Project :app declares a dependency from configuration 'compile' to configuration 'default' which is not declared in the descriptor for project

android - 获取用于调整视频大小的元数据信息

android - 2 个用户使用相同的设备和相同的应用程序但登录 ID 不同 - 他们是否有不同的 gcm 注册 ID?

安卓风格说明

Android:如何使用 AlarmManager 在特定时间查看电子邮件?