How to play an Android song?

Recently posted a question on how to get the current song playing from a music app to reveal, apparently there seems to be no easy way to do this.

Android lock screen at least 4.0+ will show the current song and will work with almost any music player including spotify. So my question is, how does this screen lock get this information?

+3


source to share


2 answers


Through a broadcast receiver, you can create a broadcast receiver and define filters for it in the manifest. Then all music apps sending this broadcast call your onReceive method as well as the information sent from the app.

EDIT:

Intent intent = new Intent("com.android.music.playstatechanged");
    intent.putExtra("playing", (mState & FLAG_PLAYING) != 0);
    if (song != null) {
        intent.putExtra("track", song.title);
        intent.putExtra("album", song.album);
        intent.putExtra("artist", song.artist);
        intent.putExtra("songid", song.id);
        intent.putExtra("albumid", song.albumId);
    }
    sendBroadcast(intent);

      



This is the code in the stock player used to send the broadcast. Look at the broadcast receiver and you can figure out how to get the data you need.

Also you need to find a filter for spotify, AFIK. This file works for soptify. "com.android.music.metachanged" or look here http://pastebin.com/ukfPnZwg

+2


source


In Android 4.0 and 4.1, the part of the lock screen that you see is called the "remote control". You can callregisterRemoteControlClient()

to provide him with information.



In Android 4.2+, you can create your own lockscreen enabled app widget.

+5


source







All Articles