在我的应用程序中,我显示了一个带有前台服务的通知,它负责播放音乐。通知由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,它会很好地显示标题和艺术家。
我的应用程序:

苹果音乐:

我的问题是,如何根据当前的实现配置这个标题和文本?谢谢。
发布于 2020-01-27 01:23:34
我只是回答我自己的问题,因为我已经找到并解决了问题。
我只设置了通知的媒体描述适配器,但实际上,媒体会话也需要设置元数据。由于我们使用的是mediaSessionConnector,所以可以通过将QueueNavigator传递给mediaSessionConnector来设置它,因此我们可以使用player实例和窗口索引来构建当前媒体的元数据。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。
if (description.subtitle != null) {
val subTitle = description.subtitle.toString()
builder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, subTitle)
builder.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_SUBTITLE, subTitle)
}发布于 2021-02-02 12:09:23
在这里,我使用METADATA_KEY_TITLE和METADATA_KEY_ARTIST作为标题和描述:
MediaMetaData data = PlayerManager.getInstance().getCurrentMediaMetaData();
Bitmap bitmap = ((BitmapDrawable) AppController.getInstance().getResources().getDrawable(R.drawable.app_logo)).getBitmap();
Bundle extras = new Bundle();
extras.putString(MediaMetadataCompat.METADATA_KEY_TITLE,data.getMediaTitle());
extras.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, data.getMediaAlbum());
return new MediaDescriptionCompat.Builder()
.setIconBitmap(bitmap)
.setExtras(extras)
.build();发布于 2019-12-02 19:14:06
您可能必须像这样构建通知:
Notification.Builder(context, channel).setContentTitle("Title").setContentText("Description").build()请在这里加上你的代码。帮助会更容易。
编辑:
您不能在适配器处返回标题:
override fun getCurrentContentTitle(player: Player?): String = "Add the title here"https://stackoverflow.com/questions/59144878
复制相似问题