Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to put STOP button - Android Auto - Legacy MediaBrowserServiceCompat #1775

Open
billyjoker opened this issue Oct 3, 2024 · 0 comments

Comments

@billyjoker
Copy link

Hi I'm trying to put the stop button in the player screen and remove all other buttons:

mMediaSession = MediaSessionCompat(
                this,
                "MediaSessionCompat_TAG"
            ).apply {
                val sessionActivityPendingIntent =
                    packageManager?.getLaunchIntentForPackage(packageName)?.let { sessionIntent ->
                        PendingIntent.getActivity(this@AudioService, 0, sessionIntent, 0)
                    }
                setSessionActivity(sessionActivityPendingIntent)
                isActive = true
                setCallback(mediaSessionCallback)
                setPlaybackState(
                    playbackStateBuilder.apply {
                        setState(PlaybackStateCompat.STATE_STOPPED, C.TIME_UNSET, 1f)
                        setActions(PlaybackStateCompat.ACTION_STOP)
                    }.build()
                )
                setSessionToken(sessionToken)
            ...
            
            In addition I put in the MediaControllerCallback the value of stop
                
                @Suppress("PropertyName")
val EMPTY_PLAYBACK_STATE: PlaybackStateCompat = PlaybackStateCompat.Builder()
    .setState(PlaybackStateCompat.STATE_STOPPED, C.TIME_UNSET, 0f)
    .build()
    
        private inner class MediaControllerCallback : MediaControllerCompat.Callback() {

        override fun onPlaybackStateChanged(state: PlaybackStateCompat?) {
            playbackState.postValue(EMPTY_PLAYBACK_STATE)
        }

        override fun onMetadataChanged(metadata: MediaMetadataCompat?) {
            playbackState.postValue(EMPTY_PLAYBACK_STATE)
            nowPlaying.postValue(
                if (metadata?.id == null) {
                    NOTHING_PLAYING
                } else {
                    metadata
                }
            )
        }

        override fun onQueueChanged(queue: MutableList<MediaSessionCompat.QueueItem>?) {
            playbackState.postValue(EMPTY_PLAYBACK_STATE)
        }

        override fun onSessionEvent(event: String?, extras: Bundle?) {
            playbackState.postValue(EMPTY_PLAYBACK_STATE)
            super.onSessionEvent(event, extras)
            when (event) {
                NETWORK_FAILURE -> networkFailure.postValue(true)
            }
        }

I only get the PLAY button and periodically a blink show and hide the previous and next buttons

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant