java - 无法使 MediaStyle 通知的媒体按钮正常工作
问题描述
我有一个PlaybackService
管理音乐播放的。当应用程序播放音乐时,它会显示通知。我已经让 MediaStyle 通知正常工作,我可以看到专辑插图和三个按钮:暂停、停止、下一步。但它们不会触发MediaSessionCompat.Callback
's onStop
onPause
onSkipToNext
。我已将会话令牌分配给通知,也设置了mediaSession!!.isActive = true
.
另外,虽然我扩展了 MediaBrowserServiceCompat 但我不需要浏览功能 atm。我只是想尝试这样做是否可以解决问题,但事实证明它不起作用。我已经阅读了很多次文档并尝试了不同的方法,但仍然没有运气..请帮帮我!
class PlaybackService : MediaBrowserServiceCompat() {
private var mediaSession: MediaSessionCompat? = null
private val stateBuilder: PlaybackStateCompat.Builder by lazy {
PlaybackStateCompat.Builder().setActions(
PlaybackStateCompat.ACTION_PLAY
or PlaybackStateCompat.ACTION_PLAY_PAUSE
or PlaybackStateCompat.ACTION_STOP
or PlaybackStateCompat.ACTION_SKIP_TO_NEXT
)
}
override fun onCreate() {
super.onCreate()
mediaSession = MediaSessionCompat(this, "debug log").apply {
setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS or MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS)
setPlaybackState(stateBuilder.build())
setCallback(object : MediaSessionCompat.Callback() {
override fun onStop() {
stop()
}
override fun onPause() {
pause()
}
override fun onSkipToNext() {
playNext()
}
})
setMediaButtonReceiver(MediaButtonReceiver.buildMediaButtonPendingIntent(this@PlaybackService, PlaybackStateCompat.ACTION_STOP))
}
sessionToken = mediaSession!!.sessionToken
}
override fun onGetRoot(clientPackageName: String, clientUid: Int, rootHints: Bundle?): BrowserRoot? {
}
override fun onLoadChildren(parentId: String, result: Result<MutableList<MediaBrowserCompat.MediaItem>>) {
}
private fun startForeground() {
mediaSession!!.isActive = true
createNotificationChannel()
var artist = PlayingAudioInfo.audio!!.artist
if (artist.isNullOrBlank())
artist = "unknown artist"
val builder = NotificationCompat.Builder(this, NOTI_CHANNEL_ID).apply {
setContentIntent(mediaSession!!.controller.sessionActivity)
// Stop the service when the notification is swiped away
setDeleteIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(this@PlaybackService, PlaybackStateCompat.ACTION_STOP))
setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
if (mNotification == null) //first time show it on status bar
setTicker(title)
getNextPlayerAudioImg()?.let {
setLargeIcon(it)
}
setContentTitle(artist!! + title!!)
setSubText(PlayingAudioInfo.audio!!.album)
val album = PlayingAudioInfo.audio!!.album.takeIf { true }
if (!album.isNullOrBlank())
setContentText(album)
setSmallIcon(android.R.drawable.ic_media_play)
// Add media control buttons that invoke intents in your media service
addAction(R.drawable.ic_stop, "Stop", MediaButtonReceiver.buildMediaButtonPendingIntent(this@PlaybackService, PlaybackStateCompat.ACTION_STOP))
addAction(R.drawable.ic_stop, "Stop",
MediaButtonReceiver.buildMediaButtonPendingIntent(this@PlaybackService, PlaybackStateCompat.ACTION_STOP))
addAction(R.drawable.ic_pause, "Pause",
MediaButtonReceiver.buildMediaButtonPendingIntent(this@PlaybackService, PlaybackStateCompat.ACTION_PLAY_PAUSE))
addAction(R.drawable.ic_skip_next, "Next",
MediaButtonReceiver.buildMediaButtonPendingIntent(this@PlaybackService, PlaybackStateCompat.ACTION_SKIP_TO_NEXT))
setStyle(
android.support.v4.media.app.NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0, 1, 2)
.setMediaSession(mediaSession!!.sessionToken)
// Add a cancel button
.setShowCancelButton(true)
.setCancelButtonIntent(
MediaButtonReceiver.buildMediaButtonPendingIntent(
this@PlaybackService,
PlaybackStateCompat.ACTION_STOP
)
)
)
}
mNotification = builder.build()
startForeground(1, mNotification)
}
}
解决方案
推荐阅读
- r - 多面 ggplot 中的 ggstatsplot 字幕输出
- python - Transfermarket 的抓取功能
- javascript - 使用jsp中的javascript禁用导航后按显示的模式
- java - 替换文本框 docx4j 中的占位符
- javascript - 将 Highchart(线)附加到 Bootstrap 模态
- javascript - JavaScript - 改进动态对象的构造
- python-3.x - Python WinError 10038 尝试对非套接字的操作进行操作
- python-3.x - 使用 OPENCV 和 PYTESSERACT 对多项选择题进行排序
- python - 如何在python中将字符串日期列表转换为日期时间
- jquery - 根据滑块值向表中添加一个类