Is it possible to connect to Spotify's MediaBrowserService?
Asked Answered
M

2

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.

Modify answered 13/2, 2020 at 20:31 Comment(6)
MediaBrowserService has nothing to do with Spotify and that "Media Controller Test" example is expected to connect to the Universal Android Music Player Sample. See MusicService.kt; Spotify would need to expose an Intent service alike that.Montherlant
@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.Modify
Probably stating the obvious but, is this not where you should begin reading? developer.spotify.com/documentation/androidCabalistic
Thanks @Simson. I'm aware of the SDK but am hoping for a general solution that will work for multiple music players.Modify
@EllenSpertus I wasn't aware about a possible white-list in Spotify's MediaBrowserService implementation, and so the initial statement was partially inaccurate.Montherlant
@Ellen Spertus ,did making it a system app resolved issue for you ?Dihedral
K
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.

Kattiekatuscha answered 20/2, 2020 at 2:4 Comment(1)
Thank you. Would you consider looking at my other recent questions?Modify
O
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/

Omphale answered 22/2, 2020 at 23:32 Comment(2)
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?Modify
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.Omphale

© 2022 - 2024 — McMap. All rights reserved.