无法连接MediaBrowserCompat

时间:2017-04-25 12:55:09

标签: android mediabrowserservicecompat

我关注Android official documentation on connecting MediaBrowserCompat,但拒绝连接,事实上,onConnected()onConnectionSuspended()onConnectionFailed()都没有被调用。

我也试过了this answer,但它没有用。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audio_player);
        ButterKnife.bind(this);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setupMediaComponents();


       // initializeViews();
    }

private void setupMediaComponents() {
        Log.d(TAG, "setupMediaComponents");
        setVolumeControlStream(AudioManager.STREAM_MUSIC);

        mediaBrowserCompat = new MediaBrowserCompat(this, new ComponentName(this, SongPlayerService.class),
                mediaBrowserCompatConnectionCallback, null);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mediaBrowserCompat.connect();
    }

    @Override
    public void onStop() {
        super.onStop();
        // (see "stay in sync with the MediaSession")
        if (MediaControllerCompat.getMediaController(this) != null) {


    MediaControllerCompat.getMediaController(this).unregisterCallback(controllerCallback);
            }
            mediaBrowserCompat.disconnect();

        }

private MediaBrowserCompat.ConnectionCallback mediaBrowserCompatConnectionCallback = new MediaBrowserCompat.ConnectionCallback() {
        @Override
        public void onConnected() {
            Log.d(TAG, "onConnected");
            try {
                // Create a MediaControllerCompat
                MediaControllerCompat mediaController =
                        new MediaControllerCompat(AudioPlayerActivity.this,  mediaBrowserCompat.getSessionToken());

                // Save the controller
                MediaControllerCompat.setMediaController(AudioPlayerActivity.this, mediaController);

                setControlClickListeners();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onConnectionSuspended() {
            Log.d(TAG, "onConnectionSuspended");
            // We were connected, but no longer :-(
        }

        @Override
        public void onConnectionFailed() {
            Log.d(TAG, "onConnectionFailed");
            // The attempt to connect failed completely.
            // Check the ComponentName!
        }
    };

我使用支持库的25.3.1版本,所有sdk工具都是最新的。

修改

SongPlayerService的一部分

@Nullable
    @Override
    public BrowserRoot onGetRoot(@NonNull String clientPackageName, int clientUid, @Nullable Bundle rootHints) {
        // Returning null means no one can connect so we’ll return something
        return new BrowserRoot(getString(R.string.app_name), null); // Name visible in Android auto, Bundle is for optional params
        // TODO: Support Android auto
    }

3 个答案:

答案 0 :(得分:2)

终于找到了问题的根源。我重写'onBind`并返回null。

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

总计删除以上行为我修好了。

答案 1 :(得分:2)

您必须实现MediaBrowserServiceCompat的两个方法。 至少:

@Nullable
@Override
public BrowserRoot onGetRoot(@NonNull String clientPackageName, int clientUid, @Nullable Bundle rootHints) {
    return new BrowserRoot(MEDIA_ID_ROOT, null);
}

@Override
public void onLoadChildren(@NonNull String parentId, @NonNull Result<List<MediaBrowserCompat.MediaItem>> result) {
    result.sendResult(null);
}

答案 2 :(得分:1)

如果您未能在 AndroidManifest 文件中声明服务,也可以调用

onConnectionFailed()。确保意图过滤器也正确。 source

<service
          android:name=".media.MusicService"
          android:enabled="true"
          android:exported="true"
          tools:ignore="ExportedService">

            <intent-filter>
                <action android:name="android.media.browse.MediaBrowserService" />
            </intent-filter>
 </service>