跟踪当前正在播放的音乐信息

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

我正在使用下面的代码来得到这个:

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(); } }; } 

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

有没有办法做到这一点?

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

 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)); } } } 

MediaStore.Audio.Media类中有许多列,您可以使用它来查找您想要的内容。

检查这是我的应用程序使用得很好

  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"); 

亚马逊的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"); } 

这对于检查额外的名字是方便的:

  Set<String> extrasList = intent.getExtras().keySet(); for (String str : extrasList) { Log.d(TAG, str); } 
 import com.example.media.R; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.widget.Toast; 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.activity_main); IntentFilter iF = new IntentFilter(); 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"); registerReceiver(mReceiver, iF); } private BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String track = intent.getStringExtra("track"); Toast.makeText(CurrentMusicTrackInfoActivity.this, track, Toast.LENGTH_SHORT).show(); } }; } 

是否有任何错误我运行在我的Xperia Sola设备,但它停止请帮助我