connect()之后未调用MediaBrowserCompat.ConnectionCallback.onConnected()

时间:2019-06-13 07:32:20

标签: java android android-mediasession

我正在为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()中设置了断点,但是线程并未暂停。因此,我可以得出结论,没有调用回调。

1 个答案:

答案 0 :(得分:0)

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
    }