如何从 ExoPlayer 获取媒体会话令牌

How to get media session token from ExoPlayer

我可以从 ExoPlayer 获取用于通知媒体样式的媒体会话令牌吗?

您可以使用此代码片段从 Exo Player 实例获取媒体会话令牌:

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

详细实现参考GitHub here

上的uamp android示例