androidandroid-servicespotifygoogle-musicmediabrowserservice

Is it possible to connect to Spotify's MediaBrowserService?


I am writing a voice assistant and would like to be able to launch search results in Spotify. To understand how to connect to Spotify and other apps, I am playing with the Google's Media Controller Test app, which is described as doing the following:

Create a simple MediaController that connects to a MediaBrowserService in order to test inter-app media controls.

This app works with the Universal Android Music Player sample, or any other app that implements the media APIs.

When I run the app, it identifies Spotify as providing a MediaBrowserService, but I get an error when I try connecting to Spotify through the app:

Android screenshot containing error: "MediaBrowser connection failed for Spotify"

Here's is the connection code:

mBrowser = new MediaBrowserCompat(this, mMediaAppDetails.componentName,
        new MediaBrowserCompat.ConnectionCallback() {
             @Override
             public void onConnected() {
                 setupMediaController();
                 mBrowseMediaItemsAdapter.setRoot(mBrowser.getRoot());
             }

             @Override
             public void onConnectionSuspended() {
                 //TODO(rasekh): shut down browser.
                 mBrowseMediaItemsAdapter.setRoot(null);
             }

             @Override
             public void onConnectionFailed() {
                 showToastAndFinish(getString(
                         R.string.connection_failed_msg, mMediaAppDetails.appName));
             }

         }, null);
mBrowser.connect();

Specifically, shortly after calling mBrowser.connect(), the callback's onConnectionFailed() method is called.

Does this imply that Spotify refuses MediaBrowserService requests from non-whitelisted apps (as described here) and that there's no way for an individual app writer to connect to Spotify's MediaBrowserService, or is there some other way to connect? FWIW, Google Play Music also rejects connections through the app.

I have also tried getting Spotify to search and play by launching an intent, but that was unsuccessful.


Solution

  • Does this imply that Spotify refuses MediaBrowserService requests from non-whitelisted apps (as described here) and that there's no way for an individual app writer to connect to Spotify's MediaBrowserService, or is there some other way to connect?

    Yes, that's exactly what onConnectionFailed means.

    You can verify this by changing UAMP's onGetRoot to return null rather than BrowserRoot(UAMP_EMPTY_ROOT, rootExtras).

    Generally, you'd need to reach out to each music player and ask them to whitelist your app (or switch to allowing all apps to control their playback) if you'd like to use the standard Android APIs and MediaBrowserCompat to connect to them.