我正在为Android创建播放器应用,并希望从任何UI类访问MediaControllerCompat。为此,我使用MediaBrowserCompat类,我正在尝试将其连接到MediaBrowserServiceCompat并使用已在MediaControllerCompat中设置的MediaSessionCompat.Token获取Service

我尝试了MediaBrowserCompat.connect() never calls onConnected or any MediaBrowserCompat.ConnectionCallback method解决方案,但没有帮助。

TortoiseActivity.java

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tortoise);

        mediaBrowser = new MediaBrowserCompat(
                TortoiseActivity.this,
                new ComponentName(TortoiseActivity.this, Service.class),
                new MediaBrowserCompat.ConnectionCallback() {
                    @Override
                    public void onConnected() {
                        try {
                            MediaSessionCompat.Token token = mediaBrowser.getSessionToken();
                            MediaControllerCompat controller = new MediaControllerCompat(TortoiseActivity.this, token);
                            MediaControllerCompat.setMediaController(TortoiseActivity.this, controller);
                        } catch (RemoteException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onConnectionFailed() {
                        Log.e("TAG", "onConnectionFailed");
                    }

                    @Override
                    public void onConnectionSuspended() {
                        Log.e("TAG", "onConnectionSuspended");
                    }
                },
                null);
        mediaBrowser.connect();
    }

Service.java
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        mediaSession = new MediaSessionCompat(this, Service.class.getSimpleName());
        setSessionToken(mediaSession.getSessionToken());

        mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
        mediaSession.setActive(true);

        mediaSession.setCallback(callback);
    }

SmallPlayerFragment.java
void initControls(Activity context) {
        MediaControllerCompat mediaController = MediaControllerCompat.getMediaController(context);
        this.transportControls = mediaController.getTransportControls();
    }

结果,我得到了NullPointer
java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v4.media.session.MediaControllerCompat$TransportControls android.support.v4.media.session.MediaControllerCompat.getTransportControls()' on a null object reference
        at ru.krivocraft.kbmp.SmallPlayerFragment.initControls(SmallPlayerFragment.java:49)
        at ru.krivocraft.kbmp.TortoiseActivity.getSmallPlayerFragment(TortoiseActivity.java:122)
        at ru.krivocraft.kbmp.TortoiseActivity.showSmallPlayerFragment(TortoiseActivity.java:248)
        at ru.krivocraft.kbmp.TortoiseActivity.access$200(TortoiseActivity.java:31)
        at ru.krivocraft.kbmp.TortoiseActivity$4.onItemClick(TortoiseActivity.java:109)

尝试获取MediaControllerCompat.TrasportControls时。 MediaControllerCompat绝对为空。我在Callback的onConnected()中设置了断点,但是线程并未暂停。因此,我可以得出结论,未调用回调。

最佳答案

https://stackoverflow.com/a/43676925/11485632-这个答案帮助我解决了问题。事实证明,您甚至无法实现自己的Binder。在onBind(Intent intent)中删除覆盖的Service.java后,开始调用回调。

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder; //remove this custom binder
    }

10-07 18:47