调用 stopForeground 时通知颜色发生变化
Notification color changes when stopForeground is called
我正在使用 exoplayer
构建音频播放器。当我的音频播放器启动时,我会附上一条通知。当玩家 paused/stopped 使用通知暂停按钮时,通知背景颜色会发生变化。这是测试代码。
我尝试在调用 stopForeground(false)
时再次在 playerNotificationManger
上设置通知颜色,但没有帮助。
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 发生时,我不希望通知颜色发生变化。
你能指出我做错了什么吗?
我想我明白了,我必须将 mediaSession 附加到它。查看下面的博客 post 对我有帮助。
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之前的任何版本上调用此方法都不会影响通知
我正在使用 exoplayer
构建音频播放器。当我的音频播放器启动时,我会附上一条通知。当玩家 paused/stopped 使用通知暂停按钮时,通知背景颜色会发生变化。这是测试代码。
我尝试在调用 stopForeground(false)
时再次在 playerNotificationManger
上设置通知颜色,但没有帮助。
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 发生时,我不希望通知颜色发生变化。 你能指出我做错了什么吗?
我想我明白了,我必须将 mediaSession 附加到它。查看下面的博客 post 对我有帮助。 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之前的任何版本上调用此方法都不会影响通知