How to get media session token from ExoPlayer

Viewed 522

can I get media session token from ExoPlayer for Notification Media Style?

1 Answers

You can use this code snippet to get media session token from Exo Player instance:

class SimpleSessionCallback(val simpleExoPlayer: SimpleExoPlayer) :
        MediaSessionCompat.Callback() {
        override fun onPlay() {
            simpleExoPlayer.playWhenReady = true
        }

        override fun onPause() {
            simpleExoPlayer.playWhenReady = false
        }

        override fun onSkipToPrevious() {
            simpleExoPlayer.seekTo(0)
        }
    }

    val playBackStateBuilder = PlaybackStateCompat.Builder().setActions(
        PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS or
        PlaybackStateCompat.ACTION_REWIND or
        PlaybackStateCompat.ACTION_PLAY or
        PlaybackStateCompat.ACTION_PAUSE or
        PlaybackStateCompat.ACTION_FAST_FORWARD
    )

    val mediaSession = MediaSessionCompat(context, context.packageName)
    mediaSession.setPlaybackState(playBackStateBuilder.build())
    mediaSession.setCallback(SimpleSessionCallback(exoPlayerInstance))
    mediaSession.isActive = true
    // You can use [mediaSession.sessionToken] for Notification Media Style

For detailed implementation, refer to uamp android sample on GitHub here

Related