我使用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
}
}
)
这是在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)
}
}
答案 0 :(得分:0)
创建自己的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!!!
}
}
}
}
然后注册:
registerReceiver(notificationReceiver, NotificationReceiver.intentFilter)
不要忘记注销它(避免内存泄漏):
unregisterReceiver(notificationReceiver)
答案 1 :(得分:0)
现在回答有点晚了,但也许会对某人有所帮助。
您可以为 playerNotificationManager 设置自定义 ControlDispatcher。使用 ControlDispatcher,您可以控制许多通知按钮。
playerNotificationManager.setUseStopAction(true)
playerNotificationManager.setControlDispatcher(object : DefaultControlDispatcher() {
override fun dispatchStop(player: Player, reset: Boolean): Boolean {
//Do whatever you want on stop button click.
Log.e("AudioPlayerService","Notification stop button clicked")
return true
}
})