【问题标题】:ExoPlayer notification re-appearing when player is paused and App is killed当播放器暂停和应用程序被杀死时,ExoPlayer 通知重新出现
【发布时间】:2020-12-03 10:01:35
【问题描述】:

我在使用 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()
    }
    //...
}

当正在播放音乐并且应用程序被终止(最近清除)、通知持续并且音乐继续播放时,它按预期工作。

但是如果暂停音乐并清除通知(应将其清除为音乐未播放),然后杀死应用程序,它会显示带有暂停按钮的不可关闭通知(不应显示)但暂停按钮不起作用并且音频也没有播放。

当应用被杀死且播放器处于暂停状态时,如何防止显示通知?

【问题讨论】:

    标签: android exoplayer exoplayer2.x


    【解决方案1】:

    我能够通过在我的服务中覆盖 onTaskRemoved 来解决此问题。

        override fun onTaskRemoved(rootIntent: Intent?) {
            super.onTaskRemoved(rootIntent)
            if (player.isPlaying == false)
                stopForeground(true)
        }
    
    

    当用户删除 App Task 时调用此函数。我们可以在这里检查是否需要我们的服务继续运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-01
      • 2017-04-12
      • 2021-09-21
      • 2018-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多