首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >媒体会话在AOD上“无标题”(始终显示)

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

Stack Overflow用户
提问于 2019-12-02 18:39:25
回答 3查看 1.7K关注 0票数 7

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

代码语言:javascript
复制
    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,它会很好地显示标题和艺术家。

我的应用程序:

苹果音乐:

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-01-27 01:23:34

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

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

代码语言:javascript
复制
    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

代码语言:javascript
复制
if (description.subtitle != null) {
    val subTitle = description.subtitle.toString()
    builder.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, subTitle)
    builder.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_SUBTITLE, subTitle)
}
票数 7
EN

Stack Overflow用户

发布于 2021-02-02 12:09:23

在这里,我使用METADATA_KEY_TITLEMETADATA_KEY_ARTIST作为标题和描述:

代码语言:javascript
复制
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();
票数 2
EN

Stack Overflow用户

发布于 2019-12-02 19:14:06

您可能必须像这样构建通知:

代码语言:javascript
复制
Notification.Builder(context, channel).setContentTitle("Title").setContentText("Description").build()

请在这里加上你的代码。帮助会更容易。

编辑:

您不能在适配器处返回标题:

代码语言:javascript
复制
 override fun getCurrentContentTitle(player: Player?): String = "Add the title here"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59144878

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档