Android get current song playing and song changed events, like Musixmatch

耗尽温柔 提交于 2019-12-03 17:33:32

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);
                 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!