Android 外部播放器通知未清除

Android 外部播放器通知未清除,android,exoplayer,exoplayer2.x,Android,Exoplayer,Exoplayer2.x,基于此,我已将exoplayer集成到我的应用程序中 我在createCurrentContentIntent()中添加了一个挂起的意图 我在这里面临一个问题。我开始播放音频,播放器通知也出现在状态栏中。我的要求是即使应用程序在后台也要播放音频。所以,我还没有在onStop()中释放播放器。我在onDestroy()中添加了以下代码 如果我在播放器播放时从后台手动关闭应用程序,通知不会消失。因此,如果我单击通知,它将与NullPointerException一起崩溃,因为MyActivity不再

基于此,我已将exoplayer集成到我的应用程序中

我在createCurrentContentIntent()中添加了一个挂起的意图

我在这里面临一个问题。我开始播放音频,播放器通知也出现在状态栏中。我的要求是即使应用程序在后台也要播放音频。所以,我还没有在onStop()中释放播放器。我在onDestroy()中添加了以下代码

如果我在播放器播放时从后台手动关闭应用程序,通知不会消失。因此,如果我单击通知,它将与NullPointerException一起崩溃,因为MyActivity不再存在


有人能为同样的问题提出解决方案吗?

我已经实现了ExoPlayer以及
MediaSessionCompat
MediaSessionConnector
,它允许Exo隐式地管理媒体通知(以及音频焦点等内容)

classmyserviceclass{
私有lateinit变量播放器:SimpleExoPlayer
私有lateinit var playerNotificationManager:playerNotificationManager
私有lateinit var mediaSession:MediaSessionCompat
私有lateinit var mediaSessionConnector:mediaSessionConnector
重写fun onCreate(){
super.onCreate()
玩家=。。。
playerNotificationManager=。。。
mediaSession=MediaSessionCompat(这个,常量)。应用{..设置回调…}
mediaSessionConnector=mediaSessionConnector(mediaSession)
mediaSessionConnector.setPlayer(播放器)
playerNotificationManager.setMediaSessionToken(mediaSession.token)
playerNotificationManager.setPlayer(播放器)
}
重写onDestroy(){
mediaSession.isActive=false
mediaSession.release()
mediaSessionConnector.setPlayer(空)
playerNotificationManager.setPlayer(空)
player.release()
super.ondestory()
}
}
这应该可以在您关闭应用程序时删除通知


我还使用PlayerNotificationReceiver对系统的通知更改做出反应,这在上面的代码中被省略了。此外,在应用程序中触发和响应通知的整个部分也被省略。

查看代码时,调用setPlayer(/*player=/null)会导致调用notificationManager。如果存在a)非null播放器集和b)通知已启动,则取消(notificationId)。所以我很确定这是按预期进行的。如果您正在前台服务中运行播放机,并且您已调用startForeground(notificationId,notification),则在您调用stopForeground(/removeNotification=*/true | | false)之前,系统不会删除通知。
    return PendingIntent.getActivity(
        context, 0,
        Intent(context, MyActivity::class.java), 0
    )
    override fun onDestroy() {
        playerNotificationManager?.setPlayer(null)
        player?.stop()
        player?.release()
        player = null
        super.onDestroy()
    }