Skip to content

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 ExtraTypeDescription
idStringA Spotify URI for the track
artistStringThe track artist
albumStringThe album name
trackStringThe track name
lengthIntegerLength 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 ExtraTypeDescription
playingBooleanTrue if playing, false if paused
playbackPositionIntegerThe 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.

Info:Note that in some countries special licenses are required to display synchronized lyrics with music. Please make sure that your application abides by any such legal requirements.

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:


_37
import android.content.BroadcastReceiver;
_37
import android.content.Context;
_37
import android.content.Intent;
_37
_37
public 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
}