WISHY
WISHY

Reputation: 11999

onLoadChildren must call detach(), MusicBrowserService, Android?

I am using MediaBrowserService to play audio on the device. When I turn on the bluetooth and play audio the app crashes.

Below is the logs

java.lang.IllegalStateException: onLoadChildren must call detach() or sendResult() before returning for package=com.android.bluetooth id=__ROOT__
   at android.service.media.MediaBrowserService.performLoadChildren(MediaBrowserService.java:669)
   at android.service.media.MediaBrowserService.addSubscription(MediaBrowserService.java:600)
   at android.service.media.MediaBrowserService.-wrap3(MediaBrowserService.java)
   at android.service.media.MediaBrowserService$ServiceBinder$3.run(MediaBrowserService.java:272)
   at android.os.Handler.handleCallback(Handler.java:751)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:154)
   at android.app.ActivityThread.main(ActivityThread.java:6682)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

How to handle this?

Upvotes: 0

Views: 1953

Answers (2)

veritas1
veritas1

Reputation: 9170

onLoadChildren()

Called to get information about the children of a media item.

Implementations must call result.sendResult with the list of children. If loading the children will be an expensive operation that should be performed on another thread, result.detach may be called before returning from this function, and then result.sendResult called when the loading is complete.

In case the media item does not have any children, call MediaBrowserService.Result.sendResult(T) with an empty list.

https://developer.android.com/reference/android/service/media/MediaBrowserService.html#onLoadChildren(java.lang.String,%20android.service.media.MediaBrowserService.Result%3Cjava.util.List%3Candroid.media.browse.MediaBrowser.MediaItem%3E%3E,%20android.os.Bundle)

One way or another result.sendResult() must be called in the onLoadChildren() implementation.

Upvotes: 1

Maxim Firsoff
Maxim Firsoff

Reputation: 2296

Seems like MediaBrowserService is handling it on a wrong state, check the lifecycle here: https://developer.android.com/guide/topics/media-apps/audio-app/building-a-mediabrowserservice#service-lifecycle

Upvotes: 0

Related Questions