Reputation: 21
I'm trying to get the duration of the song that currently playing in the music player. I'm using the code below, and able to get the artist name, track name and a few other details, but can't find the duration and elapsed time. Is there any other way to find it?
@Override
protected 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.android.music.playstatechanged");
iF.addAction("com.android.music.playbackcomplete");
iF.addAction("com.android.music.queuechanged");
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");
iF.addAction("com.android.music.playstatechanged");
iF.addAction("com.android.music.playbackcomplete");
iF.addAction("com.android.music.metachanged");
//HTC Music
iF.addAction("com.htc.music.playstatechanged");
iF.addAction("com.htc.music.playbackcomplete");
iF.addAction("com.htc.music.metachanged");
//MIUI Player
iF.addAction("com.miui.player.playstatechanged");
iF.addAction("com.miui.player.playbackcomplete");
iF.addAction("com.miui.player.metachanged");
//Real
iF.addAction("com.real.IMP.playstatechanged");
iF.addAction("com.real.IMP.playbackcomplete");
iF.addAction("com.real.IMP.metachanged");
//SEMC Music Player
iF.addAction("com.sonyericsson.music.playbackcontrol.ACTION_TRACK_STARTED");
iF.addAction("com.sonyericsson.music.playbackcontrol.ACTION_PAUSED");
iF.addAction("com.sonyericsson.music.TRACK_COMPLETED");
iF.addAction("com.sonyericsson.music.metachanged");
iF.addAction("com.sonyericsson.music.playbackcomplete");
iF.addAction("com.sonyericsson.music.playstatechanged");
//rdio
iF.addAction("com.rdio.android.metachanged");
iF.addAction("com.rdio.android.playstatechanged");
//Samsung Music Player
iF.addAction("com.samsung.sec.android.MusicPlayer.playstatechanged");
iF.addAction("com.samsung.sec.android.MusicPlayer.playbackcomplete");
iF.addAction("com.samsung.sec.android.MusicPlayer.metachanged");
iF.addAction("com.sec.android.app.music.playstatechanged");
iF.addAction("com.sec.android.app.music.playbackcomplete");
iF.addAction("com.sec.android.app.music.metachanged");
//Winamp
iF.addAction("com.nullsoft.winamp.playstatechanged");
//Amazon
iF.addAction("com.amazon.mp3.playstatechanged");
//Rhapsody
iF.addAction("com.rhapsody.playstatechanged");
//PowerAmp
iF.addAction("com.maxmpz.audioplayer.playstatechanged");
//will be added any....
//scrobblers detect for players (poweramp for example)
//Last.fm
iF.addAction("fm.last.android.metachanged");
iF.addAction("fm.last.android.playbackpaused");
iF.addAction("fm.last.android.playbackcomplete");
//A simple last.fm scrobbler
iF.addAction("com.adam.aslfms.notify.playstatechanged");
//Scrobble Droid
iF.addAction("net.jjc1138.android.scrobbler.action.MUSIC_STATUS");
registerReceiver(mReceiver, iF);
}
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extra = intent.getExtras();
Log.v("yossi", extra.toString());
}
};
Upvotes: 2
Views: 1570
Reputation: 2362
You will get duration of song by using following intents.But is supports only for few players.Please try it
duration =intent.getLongExtra(MediaStore.Audio.AudioColumns.DURATION, 0);
duration =intent.getLongExtra("duration", 0);
Upvotes: 1