1

I followed the Android official documentation on connecting MediaBrowserCompat but it's refused to connect, as a matter of fact neither onConnected(), onConnectionSuspended() or onConnectionFailed() is called.

I have also tried this answer but it didn't work.

@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!
        }
    };

I am using version 25.3.1 of the support library and all sdk tools are upto to date.


EDIT

Part of 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
    }
Community
  • 1
  • 1
X09
  • 3,827
  • 10
  • 47
  • 92
  • Are you trying to connect to your own `MediaBrowserServiceCompat` or another app's? – ianhanniballake Apr 25 '17 at 21:44
  • @ianhanniballake My bad! I didn't include the method that's doing the actual connection. I have updated the question with the method. Yes I trying to connect to ` SongPlayerService.class`, it is a service that exists in my project and extends `MediaBrowserServiceCompat`. – X09 Apr 25 '17 at 21:52
  • Can you include the `onGetRoot()` for your `SongPlayerService`? – ianhanniballake Apr 25 '17 at 22:29
  • @ianhanniballake Yes, done. – X09 Apr 25 '17 at 22:34
  • Hmm, everything seems fine. If you can build a small project that shows the same problem, it probably makes sense to [file a bug](https://issuetracker.google.com/issues/new?component=192731&template=842428) – ianhanniballake Apr 25 '17 at 22:50
  • @ianhanniballake I don't think it's a bug. I created a a small project like you adviced and it's connecting. I guess the mistake is coming from the other project. I'll have to dig deeper to find the cause. Hopefully, I'll find it and update this question with the solution. Thanks for pointing me in the right direction. – X09 Apr 26 '17 at 08:22

3 Answers3

6

Finally found the source of the problem. I was overriding onBind and returning null.

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

removing the above lines fixed it for me.

X09
  • 3,827
  • 10
  • 47
  • 92
4

You must implement two methods of MediaBrowserServiceCompat. At least:

@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);
}
Son Tieu
  • 517
  • 4
  • 15
  • Not strictly true - you only need to implement `onGetRoot` in order to establish a connection between a `MediaBrowser` and a `MediaBrowserService` – donturner Jul 14 '20 at 20:00
2

onConnectionFailed() can also be called if you fail to declare the service in the AndroidManifest file. Make sure the intent-filter is also correct. 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>
Neeraj Sewani
  • 3,952
  • 6
  • 38
  • 55