问题
I want to write an app to stream the current playing music to another device. The connection between the two devices does work and I also can stream some strings over wifi but I got problems with getting the current track.
I used the code of this blog to get some info of the current playing track, but I couldn't get the path to the file.
I think it should be possible to get the path when I even receive the album, track or artist. But I tried and tried and didn't find anything. So I'm asking: is it even possible to get the path through a receiver? If not is there another way to get the current track and its path?
回答1:
I have Searched a lot for this and I have found lots of solutions but they didn't work for me.At last I have used a combination of all existing solutions and it worked. You have to add all other players in your app.Then it will return currently playing song's title(it will return file name if your song doesn't have title) for you:
intent.getStringExtra("track")
At last query this specific song and you can access all information about this song and of course it's path.Let me if it works for you.This works for me:
public class GetMusic extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.get_music);
IntentFilter iF = new IntentFilter();
iF.addAction("com.android.music.playstatechanged");
iF.addAction("com.htc.music.playstatechanged");
iF.addAction("fm.last.android.playstatechanged");
iF.addAction("com.sec.android.app.music.playstatechanged");
iF.addAction("com.nullsoft.winamp.playstatechanged");
iF.addAction("com.amazon.mp3.playstatechanged");
iF.addAction("com.miui.player.playstatechanged");
iF.addAction("com.real.IMP.playstatechanged");
iF.addAction("com.sonyericsson.music.playstatechanged");
iF.addAction("com.rdio.android.playstatechanged");
iF.addAction("com.samsung.sec.android.MusicPlayer.playstatechanged");
iF.addAction("com.andrew.apollo.playstatechanged");
iF.addAction("gonemad.dashclock.music.playstatechanged");
iF.addAction("com.piratemedia.musicmod.playstatechanged");
iF.addAction("com.tbig.playerpro.playstatechanged");
iF.addAction("org.abrantix.rockon.rockonnggl.playstatechanged");
iF.addAction("com.maxmpz.audioplayer.playstatechanged");
iF.addAction("com.doubleTwist.androidPlayer.playstatechanged");
iF.addAction("com.lge.music.playstatechanged");
registerReceiver(mReceiver, iF);
}
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Uri mAudioUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.TITLE + " == \"" + intent.getStringExtra("track") + "\"";
String[] STAR = { "*" };
Cursor cursor = getContentResolver().query(mAudioUri,STAR, selection, null, null);
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
if (cursor.getColumnIndex(MediaStore.Audio.Media.TITLE) != -1) {
String fullpath = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DATA));
Toast.makeText(getApplicationContext(),"song path: " + fullpath,Toast.LENGTH_LONG).show();
}
}
}
};
}
来源:https://stackoverflow.com/questions/14657496/how-to-get-the-path-of-a-current-playing-track-on-android