Capture music running in another person’s app

Asked

Viewed 85 times

3

Hello, on Facebook has a button that shows which Spotify song is running. Is there a way to do that in my app? I’ve looked for information but I can not find how to do it on Android. What I want is for him to capture inside the open title and artist of performing music.

  • 2

    I think this has to see if Spotify provides via API.

  • 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.

1 answer

1

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.

  • 4

    What does this code do? How does it work? An explanation, even simple, would help a lot to make it better. The comments are in English, you could also have them translated. The good point is that you quoted the source. I wasn’t the one who voted against it, but you’d only deserve my +1 if you’d improve that answer a little bit.

  • I will try to understand how to put this in my project and as soon as I get my feedback if it worked xD

  • Didn’t work, did I do something wrong t.t I added the receiver to my android manifest, created the Mybroadcastreceiver class and pasted the code into it... I added a variable public String music_finally=""; and on receive, after picking up the songs, did music_finally=artistName+trackName; E on main activy main I did so I call the class private Mybroadcastreceiver Spotify=new Mybroadcastreceiver(); I made a function that stops 2 seconds gives an alert calling the string Spotify.music_finally It was to return the music, but the variable remains empty... Did I do something wrong?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.