在使用exoplayer的PlayerNotificationManager显示玩家通知时,我遇到了一个非常奇怪的问题。我使用以下代码在前台显示通知:
class AudioPlayerService : LifecycleService() {
private val notificationListener = object : PlayerNotificationManager.NotificationListener {
override fun onNotificationPosted(
notificationId: Int,
notification: Notification,
ongoing: Boolean
) {
super.onNotificationPosted(notificationId, notification, ongoing)
if (ongoing)
startForeground(notificationId, notification)
else
stopForeground(false)
}
}
override fun onCreate() {
super.onCreate()
playerNotificationManager = PlayerNotificationManager(
this,
CHANNEL_ID,
NOTIFICATION_ID,
descriptionAdapter,
notificationListener
).apply {
setFastForwardIncrementMs(0)
setRewindIncrementMs(0)
setUseNextAction(false)
setUsePreviousAction(false)
setUseStopAction(false)
setUseChronometer(false)
}
//...
}
override fun onDestroy() {
releasePlayer()
super.onDestroy()
}
//...
}当音乐正在播放,应用程序被终止(从最近清除),通知持续存在,音乐继续播放时,它可以按预期工作。
但是如果暂停音乐并清除通知(当音乐没有播放时应该清除),然后杀死应用程序,它会显示带有暂停按钮的不可撤销通知(不应该显示),但暂停按钮不起作用,音频也不会播放。
App被杀、播放器处于暂停状态时,如何防止通知显示?
发布于 2020-12-07 16:43:19
我能够通过在我的服务中覆盖onTaskRemoved来解决这个问题。
override fun onTaskRemoved(rootIntent: Intent?) {
super.onTaskRemoved(rootIntent)
if (player.isPlaying == false)
stopForeground(true)
}此函数在用户移除App Task时调用。我们可以在这里检查我们是否需要我们的服务来保持运行。
https://stackoverflow.com/questions/65123718
复制相似问题