使用 xamarin android 在 android 上获取当前播放曲目

get current playing track on android with xamarin android

是否可以使用 xamarin android 在 android 上获取当前正在播放的歌曲?

我尝试使用音频管理器,但我无法从此处读取当前曲目

AudioManager am = (AudioManager)Application.Context.GetSystemService(Context.AudioService);

提前致谢,

当phone播放音乐时,系统会发送广播,内容是关于音乐的信息。所以你可以使用 BroadcastReceiver 来获取它。我做了一个简单的演示来获取当前播放的曲目。

MainActivity.cs:在OnCreate方法中加入如下代码

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");
        RegisterReceiver(new MusicInfoReceiver(),iF);

MusicInfoReceiver.cs

[BroadcastReceiver]
public class MusicInfoReceiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        String action = intent.Action;
        Boolean playing = intent.GetBooleanExtra("playing", false);
        String cmd = intent.GetStringExtra("command");
      
        String artist = intent.GetStringExtra("artist");
        String album = intent.GetStringExtra("album");
        String track = intent.GetStringExtra("track");
        
    }
}