7

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.

Ellen Spertus
  • 6,576
  • 9
  • 50
  • 101
  • `MediaBrowserService` has nothing to do with Spotify and that "Media Controller Test" example is expected to connect to the [Universal Android Music Player Sample](https://github.com/android/uamp). See [MusicService.kt](https://github.com/android/uamp/blob/master/common/src/main/java/com/example/android/uamp/media/MusicService.kt); Spotify would need to expose an Intent service alike that. – Martin Zeitler Feb 16 '20 at 01:54
  • @MartinZeitler Thanks for the reply. According to the README for "Media Controller Test", it shows "apps that register a service with an intent filter action of 'android.media.browse.MediaBrowserService'". Spotify shows up in the list within the app, as shown in the screenshot I added. I've also tried using intents, as described in the updated question. – Ellen Spertus Feb 16 '20 at 23:09
  • Probably stating the obvious but, is this not where you should begin reading? https://developer.spotify.com/documentation/android/ – Simson Feb 17 '20 at 03:47
  • 2
    Thanks @Simson. I'm aware of the SDK but am hoping for a general solution that will work for multiple music players. – Ellen Spertus Feb 17 '20 at 19:46
  • @EllenSpertus I wasn't aware about a possible white-list in Spotify's `MediaBrowserService` implementation, and so the initial statement was partially inaccurate. – Martin Zeitler Feb 20 '20 at 09:47
  • @Ellen Spertus ,did making it a system app resolved issue for you ? – Sankalp Pandya May 31 '21 at 12:07

2 Answers2

6

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.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
1

You cannot connect to Spotify using MediaBrowserService unless your application is a system application signed with the Android Build key. I am not 100% sure that even that will do the trick.

However, you don't need MediaBrowserService to query Spotify. I would suggest you giving Spotify Android SDK a try: https://developer.spotify.com/documentation/android/

Yuriy Kulikov
  • 2,059
  • 1
  • 16
  • 29
  • Thanks. I tried self-signing a certificate and using it for the application, with no change in behavior. Is that what you were suggesting or something else? – Ellen Spertus Feb 23 '20 at 20:32
  • 1
    You need a platform key for this, which only the device manufacturer has. Therefore I am suggesting you to try Spotify SDK which can be used to achieve what you want. – Yuriy Kulikov Feb 24 '20 at 23:10