I'm working on an Android project that uses MediaSession and it seems no matter what I do , MediaSession.Callback's onPrepareFromMediaId function is not triggered from the Android Auto.
The onGetRoot and onLoadChildren from the media browser service are called, the interface is populated with data provided in those two functions but onPrepare... function are not called when selecting a Playable Item. If I set the playstate to stop or paused, the onPlay method of the callback interface is called which is evidence to me that the session is correctly set, but the onPrepare.. method are not called. Any Ideas ?
MediaBrowserServiceCompat derived class
...
override fun onCreate() {
super.onCreate()
...
mediaSession = MediaSessionCompat(this, "MyService").apply {
// Enable callbacks from MediaButtons and TransportControls
setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS or MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS)
setRepeatMode(PlaybackStateCompat.REPEAT_MODE_NONE)
setShuffleMode(PlaybackStateCompat.SHUFFLE_MODE_NONE)
// Set an initial PlaybackState with ACTION_PLAY, so media buttons can start the player
stateBuilder = PlaybackStateCompat.Builder()
.setActions(
PlaybackStateCompat.ACTION_PLAY or
PlaybackStateCompat.ACTION_PREPARE_FROM_MEDIA_ID or
PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID or
PlaybackStateCompat.ACTION_PREPARE_FROM_SEARCH or
PlaybackStateCompat.ACTION_PLAY_FROM_SEARCH)
.setState(PlaybackStateCompat.STATE_NONE,0,0.0f)
isActive = true
setPlaybackState(stateBuilder.build())
setMetadata(metadata)
// MySessionCallback() has methods that handle callbacks from a media controller
// mediaSessionConnector = MediaSessionConnector(this)
// mediaSessionConnector.setPlaybackPreparer(UampPlaybackPreparer())
setSessionToken(sessionToken)
setCallback(callback)
}
....
private val callback = object: MediaSessionCompat.Callback() {
override fun onCommand(command: String?, extras: Bundle?, cb: ResultReceiver?) {
}
override fun onPrepare() {
println(" onPrepare") //not called
}
override fun onPrepareFromMediaId(mediaId: String?, extras: Bundle?) {
println(" onPrepareFromMediaId $mediaId") //not called
}
override fun onPrepareFromSearch(query: String?, extras: Bundle?) {
println("onPrepareFromSearch $query") //not called
}
override fun onPrepareFromUri(uri: Uri?, extras: Bundle?) {
println(" onPrepareFromUri $uri") //not called
}
Selecting an item from Android Auto must trigger onPrepareFromMediaId of the playback preparer, in your case UampPlaybackPreparer.
So not the object that you extend from MediaSessionCompat.Callback(), but the one you extend from MediaSessionConnector.PlaybackPreparer.