如何从MediaPlayer获取当前播放歌曲的详细信息

时间:2010-09-01 10:12:10

标签: android media-player

有没有办法获取MediaPlayer播放的当前歌曲的详细信息?

4 个答案:

答案 0 :(得分:13)

仅供参考,这有效:

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.util.Log;

public class MediaPlayer extends Activity {

public static final String SERVICECMD = "com.android.music.musicservicecommand";
public static final String CMDNAME = "command";
public static final String CMDTOGGLEPAUSE = "togglepause";
public static final String CMDSTOP = "stop";
public static final String CMDPAUSE = "pause";
public static final String CMDPREVIOUS = "previous";
public static final String CMDNEXT = "next";
/** Called when the activity is first created. */
@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.d("mIntentReceiver.onReceive ", action + " / " + cmd);
String artist = intent.getStringExtra("artist");
String album = intent.getStringExtra("album");
String track = intent.getStringExtra("track");
Log.d("Music",artist+":"+album+":"+track);
}
};
}

答案 1 :(得分:6)

没有documented method从MediaPlayer实例获取当前播放文件的信息,您需要让您的应用以其他方式存储该信息。如果您正在使用服务来跟踪您的MediaPlayer实例(我建议),那么这应该不会太难。

答案 2 :(得分:0)

我认为您需要拥有自己的模型类,您可以在其中设置和获取您的歌曲标题。有关此信息和其他信息,请参阅Stack Overflow问题:
 How to set the PlayList Index for Mediaplayer(ExpressionMediaPlayer:Mediaplayer)

如果您想知道如何实施,可以获取source并了解它们的实施方式。

答案 3 :(得分:-1)

我刚刚学会了如何做到这一点,所以我不知道它是否适用于所有版本的Android。

private void getTrackInfo(Uri audioFileUri) {
    MediaMetadataRetriever metaRetriever= new MediaMetadataRetriever();
    metaRetriever.setDataSource(getRealPathFromURI(audioFileUri));
    String artist = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
    String title = metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);

}


private String getRealPathFromURI(Uri uri) {
    File myFile = new File(uri.getPath().toString());
    String s = myFile.getAbsolutePath();
    return s;
}