android - 调用 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 发生时,我不希望通知颜色发生变化。你能建议我做错什么吗?
解决方案
我想我明白了,我必须将 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 之前的任何版本上调用此方法都不会影响通知
推荐阅读
- android - Activity 中的片段中的 SetText 到 Textview
- django - How to get decrement forloop.counter in django template?
- javascript - 如何在 React 中的子组件和父组件之间共享数据?
- android - Chromium Browser Code中WebViewClient的“shouldInterceptRequest”对应的方法是什么?
- awk - compare different column of 2 different files and keep the orderin awk script
- java - 无法在 Cordova 应用程序中导入 Chunk 主题
- c# - XML 对象引用未设置为对象的实例
- c# - 提供的有效负载无法解密,因为它不受此保护提供程序的保护
- python - 在python中总结三个数据帧
- php - DataTables warning: table id=users_list - Invalid JSON response. For more information about this error, please see http://datatables.net/tn/1