【问题标题】:Media session has "no title" on AOD (Always on display)媒体会话在 AOD 上“无标题”(始终显示)
【发布时间】:2020-03-27 10:01:26
【问题描述】:

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

我的应用程序:

苹果音乐:

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

【问题讨论】:

    标签: android notifications exoplayer lockscreen


    【解决方案1】:

    我只是回答我自己的问题,因为我发现并解决了问题。

    我只设置了通知的媒体描述适配器,但实际上媒体会话也需要设置元数据。 由于我们使用的是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)
    }
    

    【讨论】:

      【解决方案2】:

      这里我使用METADATA_KEY_TITLEMETADATA_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();
      

      【讨论】:

        【解决方案3】:

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

        Notification.Builder(context, channel).setContentTitle("Title").setContentText("Description").build()
        

        请在此处添加您的代码。提供帮助会更容易。

        编辑:

        您没有在适配器返回标题:

         override fun getCurrentContentTitle(player: Player?): String = "Add the title here"
        

        【讨论】:

        • 谢谢。我已经更新了我的问题。我似乎需要为媒体会话配置一些特定字段。
        • 基本上我已经实现了所有的方法。我可以完美地看到通知,只是在 AOD 模式下我看到“没有标题”。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-30
        • 2018-02-14
        • 1970-01-01
        • 2013-02-14
        相关资源
        最近更新 更多