I am building a simple media app following the android official app. But in my case, I need to fetch URL of each clip from remote and then create a mediaId out of it and then play an audio clip. This means that I need to update my media data very frequently. For updating the media data list I am registering the custom command receiver, and it works fine it updates data and play as required. But after a calling sendCommand specific number of times (7-8) onCommand not getting called.
You can check my implementation.
Call from ViewModel
private fun sendCommandToService(metaData: List<ServiceMetaData>) {
audioConnection.sendCommand(AudioService.REFRESH_AUDIO_DATA, Bundle().apply {
putSerializable(AudioService.AUDIO_DATA, metaData as ArrayList<ServiceMetaData>)
}) { _, _ -> }
}
Service connection
fun sendCommand(
command: String,
parameters: Bundle?,
resultCallback: ((Int, Bundle?) -> Unit)
) = if (mediaBrowser.isConnected) {
mediaController.sendCommand(command, parameters, object : ResultReceiver(Handler()) {
override fun onReceiveResult(resultCode: Int, resultData: Bundle?) {
resultCallback(resultCode, resultData)
}
})
true
} else {
false
}
Media Session connector custom command receiver
override fun onCommand(
player: Player?,
controlDispatcher: ControlDispatcher?,
command: String?,
extras: Bundle?,
callback: ResultReceiver?
): Boolean =
when (command) {
REFRESH_AUDIO_DATA -> onRefreshAudioListCommandCallback(extras ?: Bundle.EMPTY, callback)
else -> false
}
This behavior is intermittent, on the emulator I faced this after repeating the same procedure 7-8 times but on a real device, this happens after 18-20 calls.