MediaBrowserCompat.connect() never calls onConnected or any MediaBrowserCompat.ConnectionCallback method

旧街凉风 提交于 2019-12-14 02:02:26

问题


I am trying to connect my activity to a MediaBrowserServiceCompat service using MediaBrowserCompat. This is my activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bush_did_nine_eleven);
    Log.d("activity", " onCreate");
    mediaBrowser = new MediaBrowserCompat(this, new ComponentName(this, MusicServiceMediaBrowser.class), new MediaBrowserCompat.ConnectionCallback() {
                @Override
                public void onConnected() {
                    Log.d("activity", "onconnected");
                    super.onConnected();
                }

                @Override
                public void onConnectionFailed() {
                    Log.d("activity", "onConnectionFailed");
                    super.onConnectionFailed();
                }

                @Override
                public void onConnectionSuspended() {
                    Log.d("activity", "onConnectionSuspended");
                    super.onConnectionSuspended();
                }
            }, null);

    mediaBrowser.connect();
    Log.d("activity", "mediaBrowser.connect() called");

}

@Override
protected void onStart() {
    super.onStart();
    Log.d("activity", "onStart");
}

@Override
protected void onStop() {
    super.onStop();
    Log.d("activity", "onStop");
    mediaBrowser.disconnect();
}

you might expect one of the ConnectionCallback methods to be called, but this is what the logcat prints (including log called from the service):

09-08 19:28:25.126 13469-13469/le1.mytube D/activity:  onCreate
09-08 19:28:25.135 13469-13469/le1.mytube D/activity: mediaBrowser.connect() called
09-08 19:28:25.137 13469-13469/le1.mytube D/activity: onStart
09-08 19:28:25.154 13469-13469/le1.mytube D/service: onCreate

it also crashes when in my service i call setSessionToken(mediaSession.getSessionToken()) with a NullPointerException :

09-08 19:34:04.817 19588-19588/le1.mytube E/AndroidRuntime: FATAL EXCEPTION: main
                                                            Process: le1.mytube, PID: 19588
                                                            Theme: themes:{}
                                                            java.lang.RuntimeException: Unable to create service le1.mytube.services.MusicServiceMediaBrowser: java.lang.NullPointerException: Attempt to invoke interface method 'void android.support.v4.media.MediaBrowserServiceCompat$MediaBrowserServiceImpl.setSessionToken(android.support.v4.media.session.MediaSessionCompat$Token)' on a null object reference
                                                                at android.app.ActivityThread.handleCreateService(ActivityThread.java:2921)
                                                                at android.app.ActivityThread.-wrap4(ActivityThread.java)
                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1446)
                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                at android.os.Looper.loop(Looper.java:148)
                                                                at android.app.ActivityThread.main(ActivityThread.java:5461)
                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                             Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void android.support.v4.media.MediaBrowserServiceCompat$MediaBrowserServiceImpl.setSessionToken(android.support.v4.media.session.MediaSessionCompat$Token)' on a null object reference
                                                                at android.support.v4.media.MediaBrowserServiceCompat.setSessionToken(MediaBrowserServiceCompat.java:999)
                                                                at le1.mytube.services.MusicServiceMediaBrowser.onCreate(MusicServiceMediaBrowser.java:46)
                                                                at android.app.ActivityThread.handleCreateService(ActivityThread.java:2911)
                                                                at android.app.ActivityThread.-wrap4(ActivityThread.java) 
                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1446) 
                                                                at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                at android.os.Looper.loop(Looper.java:148) 
                                                                at android.app.ActivityThread.main(ActivityThread.java:5461) 
                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

in my manifest i've add the necessary <action android:name="android.media.browse.MediaBrowserService"/> for the service if you are wondering.

Am I doing something wrong or is it just a bug in appcompat(v7:25.3.1)?


回答1:


In my case, I had an issue with public IBinder onBind(Intent intent) function in the service class. It should not return null. I just removed the override of the function and it solved the issue.




回答2:


This worked for me:

setSessionToken(mediaSessionCompat.getSessionToken());

and

@Nullable
@Override
public IBinder onBind(Intent intent) {
    if (SERVICE_INTERFACE.equals(intent.getAction())) {
        return super.onBind(intent);
    }
    return binder;
}


来源:https://stackoverflow.com/questions/46121952/mediabrowsercompat-connect-never-calls-onconnected-or-any-mediabrowsercompat-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!