Android 当前播放音乐的曲目信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10510292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:00:07  来源:igfitidea点击:

Track info of currently playing music

android

提问by kiran

I am implementing an application related to getting current music track information.

我正在实施一个与获取当前音乐曲目信息相关的应用程序。

I am using the following code to get that:

我正在使用以下代码来获得它:

public class CurrentMusicTrackInfoActivity extends Activity {

    public static final String SERVICECMD = "com.android.music.musicservicecommand";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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

        registerReceiver(mReceiver, iF);
    }

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            String cmd = intent.getStringExtra("command");
            Log.v("tag ", action + " / " + cmd);
            String artist = intent.getStringExtra("artist");
            String album = intent.getStringExtra("album");
            String track = intent.getStringExtra("track");
            Log.v("tag", artist + ":" + album + ":" + track);
            Toast.makeText(CurrentMusicTrackInfoActivity.this, track, Toast.LENGTH_SHORT).show();
        }
    };

}

It is working fine for some mobiles only. I want to implement code to get current music track info in all Android devices (e.g. HTC and Samsung devices).

它仅适用于某些手机。我想实现代码以获取所有 Android 设备(例如 HTC 和三星设备)中的当前音乐曲目信息。

Is there any way of doing this?

有没有办法做到这一点?

采纳答案by JMRboosties

This might help. I use this to get metadata on audio files in one of my apps:

这可能会有所帮助。我用它来获取我的一个应用程序中音频文件的元数据:

String scheme = mAudioUri.getScheme();
String title = "";
String artist = "";
if(scheme.equals("content")) {
    String[] proj = {MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST};
    Cursor cursor = this.getContentResolver().query(mAudioUri, proj, null, null, null);
    if(cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();
        if(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE) != -1) {  
            title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
            artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
        }
    }
}

There are lots of columns in the MediaStore.Audio.Media class which you can use to find what you're looking for I think.

MediaStore.Audio.Media 类中有很多列,您可以使用它们来查找我认为要查找的内容。

回答by hderanga

Check this this is what am used in my app working well

检查这是在我的应用程序中使用的运行良好的

    iF.addAction("com.android.music.metachanged");

    iF.addAction("com.htc.music.metachanged");

    iF.addAction("fm.last.android.metachanged");
    iF.addAction("com.sec.android.app.music.metachanged");
    iF.addAction("com.nullsoft.winamp.metachanged");
    iF.addAction("com.amazon.mp3.metachanged");     
    iF.addAction("com.miui.player.metachanged");        
    iF.addAction("com.real.IMP.metachanged");
    iF.addAction("com.sonyericsson.music.metachanged");
    iF.addAction("com.rdio.android.metachanged");
    iF.addAction("com.samsung.sec.android.MusicPlayer.metachanged");
    iF.addAction("com.andrew.apollo.metachanged");

回答by chughes

Amazon MP3 (Build 2.8.1) seems to be prefixing the extras with com.amazon.mp3 so for com.amazon.mp3.metachanged I used:

亚马逊 MP3 (Build 2.8.1) 似乎用 com.amazon.mp3 作为额外内容的前缀,所以对于 com.amazon.mp3.metachanged 我使用了:

    String artist = "";
    String track = "";
    if (action.equals("com.amazon.mp3.metachanged")) {
        artist = intent.getStringExtra("com.amazon.mp3.artist");
        track = intent.getStringExtra("com.amazon.mp3.track");
    } else {
        artist = intent.getStringExtra("artist");
        track = intent.getStringExtra("track");

    }

This was handy for checking the extra names:

这对于检查额外的名称非常方便:

    Set<String> extrasList = intent.getExtras().keySet();

    for (String str : extrasList) {
        Log.d(TAG, str);
    }