The example below shows how to receive notifications from the running media on Spotify. For this to work you need to enable your class to receive such notifications and this is done via Androidmanifest.xml:
<receiver
android:name="MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.spotify.music.playbackstatechanged"/>
<action android:name="com.spotify.music.metadatachanged"/>
<action android:name="com.spotify.music.queuechanged"/>
</intent-filter>
</receiver>
After the recipient has been registered, notifications will be sent to the receiving class.
You can receive information about which artist, album, track and duration is running.
Follow example code:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyBroadcastReceiver extends BroadcastReceiver {
static final class BroadcastTypes {
static final String SPOTIFY_PACKAGE = "com.spotify.music";
static final String PLAYBACK_STATE_CHANGED = SPOTIFY_PACKAGE + ".playbackstatechanged";
static final String QUEUE_CHANGED = SPOTIFY_PACKAGE + ".queuechanged";
static final String METADATA_CHANGED = SPOTIFY_PACKAGE + ".metadatachanged";
}
@Override
public void onReceive(Context context, Intent intent) {
// This is sent with all broadcasts, regardless of type. The value is taken from
// System.currentTimeMillis(), which you can compare to in order to determine how
// old the event is.
long timeSentInMs = intent.getLongExtra("timeSent", 0L);
String action = intent.getAction();
if (action.equals(BroadcastTypes.METADATA_CHANGED)) {
String trackId = intent.getStringExtra("id");
String artistName = intent.getStringExtra("artist");
String albumName = intent.getStringExtra("album");
String trackName = intent.getStringExtra("track");
int trackLengthInSec = intent.getIntExtra("length", 0);
// Do something with extracted information...
} else if (action.equals(BroadcastTypes.PLAYBACK_STATE_CHANGED)) {
boolean playing = intent.getBooleanExtra("playing", false);
int positionInMs = intent.getIntExtra("playbackPosition", 0);
// Do something with extracted information
} else if (action.equals(BroadcastTypes.QUEUE_CHANGED)) {
// Sent only as a notification, your app may want to respond accordingly.
}
}
}
Further information can be obtained here.
I think this has to see if Spotify provides via API.
– user28595
And have you tried anything? Have you looked at the Spotify API, as my colleague @diegofm mentions? You say you can’t find how to do this "on Android", but already found how to do it on another platform? Provide these details as they help you get a better and faster answer.
– Luiz Vieira