首页 > 解决方案 > 调用 stopForeground 时通知颜色发生变化

问题描述

我正在使用exoplayer构建音频播放器。当我的音频播放器启动时,我会附上一个通知。当使用通知暂停按钮暂停/停止播放器时,通知背景颜色会发生变化。这是测试代码。

playerNotificationManger我尝试在被调用时再次设置通知颜色,stopForeground(false)但没有帮助。

            if (it == StreamState.STOPPED) {
                this.stopForeground(false)
            }

            else if (it == StreamState.STARTED) {
                    val channelId = "media_playback_channel"
                    exoNotificationListener = ExoNotificationListener(onNotificationPosted, onNotificationCancelled)
                    playerNotificationManager = playerNotificationManager ?: PlayerNotificationManager.createWithNotificationChannel(
                        application.applicationContext,
                        channelId,
                        R.string.media_playback_notification,
                        R.string.media_playback_notification_id,
                        mcNotificationManager,
                        exoNotificationListener
                    )
                    playerNotificationManager?.setFastForwardIncrementMs(0)
                    playerNotificationManager?.setRewindIncrementMs(0)
                    playerNotificationManager?.setUseNavigationActions(false)
                    // Here I am setting the color which works fine when content is playing.
                    playerNotificationManager?.setColor(notificationColor)
                    bindPlayer(playerNotificationManager)
            }

当我再次开始播放时,通知颜色又回来了。当 stopForeground 发生时,我不希望通知颜色发生变化。你能建议我做错什么吗?

标签: androidexoplayer2.x

解决方案


我想我明白了,我必须将 mediaSession 附加到它。在这里查看下面的博客文章对我有帮助。 https://medium.com/google-exoplayer/the-mediasession-extension-for-exoplayer-82b9619deb2d

更新代码:

    val session = MediaSessionCompat(application.applicationContext, channelId)
    val connector = MediaSessionConnector(session)
    connector.setPlayer(exoPlayer)
    playerNotificationManager?.setMediaSessionToken(session.sessionToken)

实际上这个文档帮助了我: https ://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder#setcolorized 设置这个通知是否应该被着色。设置后,使用 setColor(int) 设置的颜色将用作此通知的背景颜色。

这应该只用于高优先级的正在进行的任务,如导航、正在进行的呼叫或用户的其他类似的高优先级事件。

对于大多数样式,仅当通知用于前台服务通知时才会应用着色。

但是,对于附加了媒体会话的 MediaStyle 和 DecoratedMediaCustomViewStyle 通知,没有这样的要求。

在 O 之前的任何版本上调用此方法都不会影响通知


推荐阅读