Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct Spotify action for a now playing intent filter?

Short Version
The Spotify Android application introduced a 'Device Broadcast Status' switch in a recent release, I want to receive those broadcasts in my application. What broadcasts are available and how should I configure my BroadcastReceiver?

Long rambling version
I am integrating a now playing feature into my Android Application and have it working with most major audio media players. I wanted to hook it into Spotify as well as this is what I primarily use to play music.

I saw a couple of other Stack Overflow posts about achieving this (How is it possible to know what's spotify is playing from an external Android app? and Intercept / Get Data sent to Spotify Widget on Android for example) and I also used the adb shell dumpsys activity broadcasts command to identify what I believe are the two actions that I am interested in

com.spotify.mobile.android.playbackstatechanged and com.spotify.mobile.android.metadatachanged

I have used the Device Broadcast Status switch from within the Spotify application itself and I am happy that the switch de/activates these broadcast events.

The problem that I am having is that the messages only seem to be sent sporadically. I receive playstatechanged events when you might expect them (play, pause, next, previous) but they do not contain any information pertinent to the current track. metadatachanged events come through fairly infrequently and with no discernible pattern. Combined this makes it very difficult (read, impossible) to know which track (if any) is currently playing. On a good session things can all go well for hours, metadatachanged events triggered whenever a new song starts, at other times, I might only get one event during a whole session.

I did notice that a flurry of events can be triggered by switching WiFi On/Off, and (possibly) interestingly if I listen to the starred playlist then I get significantly more sensible results.

Some other posts suggested that I should be able to use the standard Android actions com.android.music.playbackcomplete, com.android.music.playstatechanged and com.android.music.metachanged but my version of the Spotify application did not appear to trigger these (I am using them for integration with the standard media player).

I am aware that this is a new feature in the Spotify application and as yet undocumented, but has anyone had any success on integrating this feature into their applications? If so can you share the configuration of your BroadcastReceiver. Or, if there is another better approach, I'd love to hear that too.

The version of the Spotify applicton that I am using is 0.7.4.606.

like image 575
JohnMark13 Avatar asked Nov 27 '22 08:11

JohnMark13


1 Answers

I've gotten the following Intent Filter actions to work using Spotify 2.7.0.883:

   <!-- AndroidManifest.xml -->
   ...
   <receiver
        android:name=".MyBroadcastReceiver"
        android:enabled="true"
        android:exported="true" >

        <intent-filter>

            <!-- Spotify -->
            <action android:name="com.spotify.music.playbackstatechanged" />
            <action android:name="com.spotify.music.metadatachanged" />
            <action android:name="com.spotify.music.queuechanged" />

        </intent-filter>

    </receiver>

As far as the bundled Intent extras, this Spotify developer article is useful (But beware their incorrect Intent Filter actions as of this posting).

like image 130
dbro Avatar answered Dec 28 '22 05:12

dbro