Android Media Notifications Beta
If you are developing an Android application and want to know what is happening in the Spotify app, you can subscribe to broadcast notifications from it.
The Spotify app can posts sticky media broadcast notifications that can be read by any app on the same Android device. The media notifications contain information about what is currently being played in the Spotify App, as well as the playback position and the playback status of the app.
Enabling Media Notifications
The media notifications feature of the Spotify app may be disabled by the user for potential privacy concerns. For new or upgrading users of the Spotify app, this feature is disabled unless the user has enabled Facebook scrobbling.
It is not possible for your app to programmatically configure Spotify to enable
broadcasting. If your app does not find any broadcast messages, consider
showing the user a message asking them to enable this feature by turning
Device Broadcast Status to ON
in the Spotify app’s settings.
Event Types
The following type of events are sent with these respective intent extras:
- A metadata change intent is sent when a new track starts playing. It uses the intent action
com.spotify.music.metadatachanged
, and contains the following intent extras:
Intent Extra | Type | Description |
---|---|---|
id | String | A Spotify URI for the track |
artist | String | The track artist |
album | String | The album name |
track | String | The track name |
length | Integer | Length of the track, in seconds |
- A playback state change is sent whenever the user presses play/pause, or when seeking the track position. It uses the intent action
com.spotify.music.playbackstatechanged
and contains the following intent extras:
Intent Extra | Type | Description |
---|---|---|
playing | Boolean | True if playing, false if paused |
playbackPosition | Integer | The current playback position in milliseconds |
- A queue change is sent whenever the play queue is changed. It uses the intent action
com.spotify.music.queuechanged
and does not contain any additional intent extras.
In addition to the respective intent extras noted above, all broadcasts sent by
Spotify contain an additional extra named timeSent
(Long), which is the value
of system.currentTimeMillis()
at the time the broadcast was posted to the
system. Since broadcasts can take a bit of time to propagate to your app’s
BroadcastReceiver
, this can be used to synchronize your app more precisely
with the Spotify app. Also it is possible that the last value posted by Spotify
is quite old, your app should account for this case if necessary.
Example
This example code shows how to read media notifications. For it to work, the
app must first create a broadcast receiver which can be done in the
AndroidManifest.xml
file:
_12<receiver_12 android:name="MyBroadcastReceiver"_12 android:enabled="true"_12 android:exported="true">_12_12 <intent-filter>_12 <action android:name="com.spotify.music.playbackstatechanged"/>_12 <action android:name="com.spotify.music.metadatachanged"/>_12 <action android:name="com.spotify.music.queuechanged"/>_12 </intent-filter>_12_12</receiver>
You can also register the broadcast receiver from in your Activity
or
Fragment
(for more information about this, see the official Android
documentation for BroadcastReceiver. Once the receiver has been registered, broadcasts will
be sent to the class:
_37import android.content.BroadcastReceiver;_37import android.content.Context;_37import android.content.Intent;_37_37public class MyBroadcastReceiver extends BroadcastReceiver {_37 static final class BroadcastTypes {_37 static final String SPOTIFY_PACKAGE = "com.spotify.music";_37 static final String PLAYBACK_STATE_CHANGED = SPOTIFY_PACKAGE + ".playbackstatechanged";_37 static final String QUEUE_CHANGED = SPOTIFY_PACKAGE + ".queuechanged";_37 static final String METADATA_CHANGED = SPOTIFY_PACKAGE + ".metadatachanged";_37 }_37_37 @Override_37 public void onReceive(Context context, Intent intent) {_37 // This is sent with all broadcasts, regardless of type. The value is taken from_37 // System.currentTimeMillis(), which you can compare to in order to determine how_37 // old the event is._37 long timeSentInMs = intent.getLongExtra("timeSent", 0L);_37_37 String action = intent.getAction();_37_37 if (action.equals(BroadcastTypes.METADATA_CHANGED)) {_37 String trackId = intent.getStringExtra("id");_37 String artistName = intent.getStringExtra("artist");_37 String albumName = intent.getStringExtra("album");_37 String trackName = intent.getStringExtra("track");_37 int trackLengthInSec = intent.getIntExtra("length", 0);_37 // Do something with extracted information..._37 } else if (action.equals(BroadcastTypes.PLAYBACK_STATE_CHANGED)) {_37 boolean playing = intent.getBooleanExtra("playing", false);_37 int positionInMs = intent.getIntExtra("playbackPosition", 0);_37 // Do something with extracted information_37 } else if (action.equals(BroadcastTypes.QUEUE_CHANGED)) {_37 // Sent only as a notification, your app may want to respond accordingly._37 }_37 }_37}