How to show Android Media Controls?

Viewed 140

How can I implement android media controls for the Notification Center when using MediaPlayer?

I tried to use the offical documentation right here and this article but I couldn't implement it correctly.

enter image description here

I tried to call this method when I start my media player mediaPlayer?.start(), but nothing is showing up in the notification center:

val mSession: MediaSession by lazy { MediaSession(requireContext(), "MusicService") }

fun createMediaControls() {
    mSession.isActive = true
    mSession.setCallback(object : MediaSession.Callback() {
        override fun onMediaButtonEvent(mediaButtonIntent: Intent): Boolean {
            print("onMediaButtonEvent called: $mediaButtonIntent")
            return false
        }

        override fun onPause() {
            print("onPause called (media button pressed)")
            super.onPause()
        }

        override fun onPlay() {
            print("onPlay called (media button pressed)")
            super.onPlay()
        }

        override fun onStop() {
            print("onStop called (media button pressed)")
            super.onStop()
        }
    })
    mSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS or MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS)

    mSession.setMetadata(
        MediaMetadata.Builder()
            .putString(MediaMetadata.METADATA_KEY_TITLE, "titleee")
            .putString(MediaMetadata.METADATA_KEY_ARTIST, "artist")
            .putString(MediaMetadata.METADATA_KEY_ALBUM_ART_URI, "albumArtUri")
            .build()
    )
}
0 Answers
Related