首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >播放器暂停、App被杀时再次出现ExoPlayer通知

播放器暂停、App被杀时再次出现ExoPlayer通知
EN

Stack Overflow用户
提问于 2020-12-03 18:01:36
回答 1查看 357关注 0票数 1

在使用exoplayer的PlayerNotificationManager显示玩家通知时,我遇到了一个非常奇怪的问题。我使用以下代码在前台显示通知:

代码语言:javascript
复制
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被杀、播放器处于暂停状态时,如何防止通知显示?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-07 16:43:19

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

代码语言:javascript
复制
    override fun onTaskRemoved(rootIntent: Intent?) {
        super.onTaskRemoved(rootIntent)
        if (player.isPlaying == false)
            stopForeground(true)
    }

此函数在用户移除App Task时调用。我们可以在这里检查我们是否需要我们的服务来保持运行。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65123718

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档