java – Capture music running in someone else's app

Question:

Hi there, on Facebook there is a button that shows which song from Spotify is playing. Is it possible to do this in my app? I've already looked for information but I can't find how to do this on Android. What I want is for it to capture the title and artist of the currently running song inside Spotify open.

Answer:

The example below shows how to receive notifications from media running on Spotify. For this to work you need to enable your class so that it receives 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 recipient class.

You can receive information on which artist, album, track and duration is being played.

Here's an 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.
        }
    }
}

More information can be obtained here .

Scroll to Top