Android获取系统播放音乐信息

如题,需要获取播放的音乐信息,原理是播放音乐的时候会发送四种广播,因此可以使用广播接收器拦截一下广播……
但是!!!!当系统正在播放音乐的时候,我还不知道怎么获取音乐信息QAQ
要是哪位知道怎么获取,求告知QAQ

MusicBroadcastReceiver mbr = null;

class MusicBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        artistName = intent.getStringExtra("artist");
        musicName = intent.getStringExtra("track");
        Log.d(TAG, "onReceive:    " + intent.toString());
        tvMainMusicArtist.setText("—  " + artistName + "  —");
        tvMainMusicName.setText(musicName);

        //此处是用来查看intent中包含的key
        Bundle bundle = intent.getExtras();
        Set set = bundle.keySet();
        for (Iterator iterator = set.iterator(); iterator.hasNext(); ) {
            String key = (String) iterator.next();
            Object value = bundle.get(key);
            Log.i("IntentActivity", "key:" + key + " value:" + value);
        }
    }
}

注册一下广播

 @Override
    protected void onResume() {

        mbr = new MusicBroadcastReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.android.music.metachanged");
        intentFilter.addAction("com.android.music.queuechanged");
        intentFilter.addAction("com.android.music.playbackcomplete");
        intentFilter.addAction("com.android.music.playstatechanged");

        registerReceiver(mbr, intentFilter);


}

最后别忘了注销

protected void onDestroy() {
        if (mbr != null) {
            unregisterReceiver(mbr);
        }
        super.onDestroy();
}

最后,附intent中的jian键值对:

Android获取系统播放音乐信息_第1张图片

你可能感兴趣的:(Android相关,解决方案,学习日志,Android获取音乐信息)