问题描述
在我的应用中,我显示了一个前台服务的通知,该服务负责播放音乐.该通知由 com.google.android.exoplayer2.ui.PlayerNotificationManager
android.support.v4.media.session.MediaSessionCompat
com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector
In my app I displayed a notification with a foreground service, which is in charge of playing music. The notification is handled bycom.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,它会很好地显示标题和艺术家.
When the screen is on, there is no problem displaying the content title and text. But when I lock screen and in AOD mode, on my Pixel 3 I see a "No title" displayed. But if I use Apple Music, it displays the title and artists very well.
我的应用程序:
Apple音乐:
我的问题是,如何根据当前的实现配置此标题和文本?谢谢.
My question is, how can I configure this title and text based on my current implementation? Thanks.
推荐答案
我只是回答自己的问题,因为我已经找到并解决了问题.
I just answer my own question because I have found and solved the problem.
我只设置了通知的媒体描述适配器,但实际上,媒体会话也需要设置元数据.由于我们使用的是 mediaSessionConnector
,因此可以通过将 QueueNavigator
传递给 mediaSessionConnector
来进行设置,因此我们可以使用播放器实例和窗口索引来建立当前媒体的元数据.e.x:
I have only set the notification's media description adapter, but in fact, the media session needs to set metadata too.Since we are using mediaSessionConnector
, it can be setup by passing a QueueNavigator
to the mediaSessionConnector
, so we can use the player instance and window index to build current media's metadata. e.x:
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
.
Another point is, by default the mediaSessionConnector
use MediaSessionConnector.DefaultMediaMetadataProvider
. It doesn't setup METADATA_KEY_ARTIST
which will be used in AOD mode as the artist. So I have created my own MediaMetadataProvider, adding 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)
}
这篇关于媒体会话具有“无标题".在AOD上(始终显示)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!