Android - 获取当前播放的歌曲持续时间和已用时间

时间:2015-01-03 16:18:08

标签: android broadcastreceiver intentfilter

我正在尝试获取当前在音乐播放器中播放的歌曲的持续时间。 我正在使用下面的代码,并且能够获取艺术家姓名,曲目名称和一些其他详细信息,但无法找到持续时间和已用时间。有没有其他方法可以找到它?

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

    }
};

1 个答案:

答案 0 :(得分:1)

你将通过使用以下意图来获得歌曲的持续时间。但是仅支持少数玩家。请试一试

duration    =intent.getLongExtra(MediaStore.Audio.AudioColumns.DURATION, 0);
duration    =intent.getLongExtra("duration", 0);