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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3616956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:00:25  来源:igfitidea点击:

How to get current playing song details from MediaPlayer

androidmedia-player

提问by Eby

Is there any way to get the details of current song played by MediaPlayer?

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

回答by JohnUopini

Just for reference, this works:

仅供参考,这有效:

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

回答by fredley

There is no documented methodfor getting info on the currently playing file from a MediaPlayer instance, you'll need to make your app store that information itself in another way. If you're using a Service to keep track of your MediaPlayer instance (which I'd recommend) then this shouldn't be too hard.

没有用于从 MediaPlayer 实例获取有关当前播放文件的信息的文档化方法,您需要让您的应用程序以另一种方式存储该信息本身。如果您使用 Service 来跟踪您的 MediaPlayer 实例(我建议这样做),那么这应该不会太难。

回答by DeRagan

I think you need to have your own model classes from wherein you can set and get your song title. For this and other information, see Stack Overflow question:
How to set the PlayList Index for Mediaplayer(ExpressionMediaPlayer:Mediaplayer).

我认为您需要拥有自己的模型类,您可以从中设置和获取歌曲名称。有关此信息和其他信息,请参阅堆栈溢出问题:
如何为 Mediaplayer(ExpressionMediaPlayer:Mediaplayer) 设置播放列表索引

If you are keen to know how it is being implemented, you can get the sourceand see how they have implemented.

如果您想知道它是如何实施的,您可以获取源代码并查看它们是如何实施的。

回答by Toxid

I learned how to do it just now, so I don't know if it works on all versions of android yet.

我刚刚学会了怎么做,所以我不知道它是否适用于所有版本的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;
}