Search code examples
androidmediabrowser

Fatal Exception: java.lang.IllegalStateException in MediaBrowser


My audio streaming app have this issue in Crashlytics, with the stacktrace below

Fatal Exception: java.lang.IllegalStateException: getExtras() called while not connected (state=CONNECT_STATE_CONNECTING)
   at android.media.browse.MediaBrowser.getExtras(MediaBrowser.java:296)
   at android.support.v4.media.MediaBrowserCompatApi21.getExtras(SourceFile:65)
   at android.support.v4.media.MediaBrowserCompat$MediaBrowserImplApi21.onConnected(SourceFile:1852)
   at android.support.v4.media.MediaBrowserCompat$ConnectionCallback$StubApi21.onConnected(SourceFile:649)
   at android.support.v4.media.MediaBrowserCompatApi21$ConnectionCallbackProxy.onConnected(SourceFile:102)
   at android.media.browse.MediaBrowser$5.run(MediaBrowser.java:577)
   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:6316)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)

In my code, I do start a MediaBrowser

mMediaBrowserCompat = new MediaBrowserCompat(this, new ComponentName(this, BackgroundAudioService.class),
            mMediaBrowserCompatConnectionCallback, input);

I also provided a connection callback

private MediaBrowserCompat.ConnectionCallback mMediaBrowserCompatConnectionCallback = new MediaBrowserCompat.ConnectionCallback() {

    @Override
    public void onConnected() {
        super.onConnected();
        try {

            mMediaControllerCompat = new MediaControllerCompat(Player2Activity.this, mMediaBrowserCompat.getSessionToken());
            mMediaControllerCompat.registerCallback(mMediaControllerCompatCallback);
            MediaControllerCompat.setMediaController(Player2Activity.this,mMediaControllerCompat);

        } catch (RemoteException e) {

        }
    }



    public void onConnectionSuspended() {
        Log.v(TAG, "mMediaBrowserCompatConnection onConnectionSuspended");
        mMediaBrowserCompat = null;
    }

};

This issue is so confusing to me because, I do not see my callback function being called in the stacktrace. Furthermore, I can see the onConnected() functions already being called in the stacktrace, however, why is it still complaining the state is still CONNECT_STATE_CONNECTING. Hope someone can help me out here.

Thanks in advance.


Solution

  • It happened for me when I clicked on the button more than once while starting and Audio. MediaBrowserCompat object gets initialised multiple times which causes the error.

    While initialising the object just put a null check :

    if(mMediaBrowser == null)
                    mMediaBrowser = new MediaBrowserCompat(context, new ComponentName(context, MusicService.class), mConnectionCallback, null);
    

    Hope that works.