首页 > 解决方案 > 无法使 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)
    }

}

标签: javaandroidkotlin

解决方案


推荐阅读