What i want to achieve is very similar to what Musixmatch is doing.
I need to be informed when the music starts playing and when the song is changed. All this in the service because my app may be closed (even musicmatch does this).
In the above case even if Musixmatch app is not running i get a notification when ever i change the song on spotify or any player.
Can anyone pls tell me how to achieve this. I don't need the code help, just what to know how to achieve it. I'm really confused how Musixmatch is doing it.
Here is a solution to find if music player is playing.
AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
if(manager.isMusicActive())
{
// Something is being played.
}
then You can get information from music player what song is currently playing. Paste code below to your onCreate method in your activity class.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
IntentFilter iF = new IntentFilter();
// Read action when music player changed current song
// I just try it with stock music player form android
// stock music player
iF.addAction("com.android.music.metachanged");
// MIUI music player
iF.addAction("com.miui.player.metachanged");
// HTC music player
iF.addAction("com.htc.music.metachanged");
// WinAmp
iF.addAction("com.nullsoft.winamp.metachanged");
// MyTouch4G
iF.addAction("com.real.IMP.metachanged");
registerReceiver(mReceiver, iF);
}
In your activity class create new BroadcastReceiver mReceiver.
BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent intent) {
String action = intent.getAction();
String cmd = intent.getStringExtra("command");
Log.d("mIntentReceiver.onReceive ", action + " / " + cmd);
String artist = intent.getStringExtra("artist");
String album = intent.getStringExtra("album");
String track = intent.getStringExtra("track");
Log.d("Music", artist + ":" + album + ":" + track);
}
来源:https://stackoverflow.com/questions/34389404/android-get-current-song-playing-and-song-changed-events-like-musixmatch