问题描述
我想要实现的目标与 Musixmatch 正在这样做.
What i want to achieve is very similar to what Musixmatch is doing.
我需要在音乐开始播放和歌曲更改时得到通知.服务中的所有这些内容都是因为我的应用程序可能已关闭(即使使用musicmatch也是如此).
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).
在上述情况下,即使Musixmatch应用未运行,只要在Spotify或任何播放器上更改歌曲,我都会收到通知.
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.
任何人都可以告诉我如何实现这一目标.我不需要代码帮助,只需要知道如何实现它即可.我真的很困惑Musixmatch的工作方式.
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.
}
然后,您可以从音乐播放器中获取当前正在播放的歌曲的信息.将下面的代码粘贴到活动类中的onCreate方法中.
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);
}
在您的活动类中,创建新的BroadcastReceiver mReceiver.
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);
}
这篇关于Android获得当前的歌曲播放和歌曲更改事件,例如Musixmatch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!