我如何接收 PlayerNotificationManager 通知按钮的操作(例如停止)?

P A*_*sai 6 android android-notifications exoplayer

我使用 Exoplayer 并与 PlayerNotificationManager 绑定来处理通知上的玩家操作。它工作得非常好,但我想在从通知中按下停止按钮时得到一个侦听器或接收器。现在,当我单击停止按钮时,播放器被卡住了。

playerNotificationManager = PlayerNotificationManager.createWithNotificationChannel(
        this,
        "playback_channel",
        R.string.exo_download_notification_channel_name,
        1,
        object : PlayerNotificationManager.MediaDescriptionAdapter {
            override fun createCurrentContentIntent(player: Player?): PendingIntent? {
                val intent = Intent(context, PlayerExoActivity::class.java)
                return PendingIntent.getActivity(
                    context,
                    1,
                    intent,
                    PendingIntent.FLAG_UPDATE_CURRENT
                )
            }

            override fun getCurrentContentText(player: Player?): String? {
                return "Day " + chapterName
            }

            override fun getCurrentContentTitle(player: Player?): String {
                return courseName!!
            }

            override fun getCurrentLargeIcon(
                player: Player?,
                callback: PlayerNotificationManager.BitmapCallback?
            ): Bitmap? {
                return largeIcon
            }
        }
    )
Run Code Online (Sandbox Code Playgroud)

这是一个接收器,用于在 Exoplayer 状态更改时处理其他事情。

override fun onPlayerStateChanged(playWhenReady: Boolean, playbackState: Int) {
    if (playbackState == ExoPlayer.STATE_BUFFERING) {
        val intent = Intent("com.example.exoplayer.PLAYER_STATUS")
        intent.putExtra("state", PlaybackStateCompat.STATE_BUFFERING)
        broadcaster?.sendBroadcast(intent)
    } else if (playbackState == ExoPlayer.STATE_READY) {
        val intent = Intent("com.example.exoplayer.PLAYER_STATUS")
        if (playWhenReady) {
            intent.putExtra("state", PlaybackStateCompat.STATE_PLAYING)
        } else {
            intent.putExtra("state", PlaybackStateCompat.STATE_PAUSED)
        }
        broadcaster?.sendBroadcast(intent)
    } else if (playbackState == ExoPlayer.STATE_ENDED) {
        val intent = Intent("com.example.exoplayer.PLAYER_STATUS")
        intent.putExtra("state", PlaybackStateCompat.STATE_STOPPED)
        broadcaster?.sendBroadcast(intent)
    }
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*cki 4

创建您自己的 BroadcastReceiver 实现:

class NotificationReceiver : BroadcastReceiver() {

  companion object {
    val intentFilter = IntentFilter().apply {
      addAction(PlayerNotificationManager.ACTION_NEXT)
      addAction(PlayerNotificationManager.ACTION_PREVIOUS)
      addAction(PlayerNotificationManager.ACTION_PAUSE)
      addAction(PlayerNotificationManager.ACTION_STOP)
    }
  }

  override fun onReceive(context: Context?, intent: Intent?) {
    when (intent?.action) {
      PlayerNotificationManager.ACTION_NEXT -> {
      }
      PlayerNotificationManager.ACTION_PREVIOUS -> {
      }
      PlayerNotificationManager.ACTION_PAUSE -> {
      }
      PlayerNotificationManager.ACTION_STOP -> {
          //do what you want here!!!
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

然后注册:

registerReceiver(notificationReceiver, NotificationReceiver.intentFilter)
Run Code Online (Sandbox Code Playgroud)

不要忘记注销它(避免内存泄漏):

unregisterReceiver(notificationReceiver)
Run Code Online (Sandbox Code Playgroud)