Home > Back-end >  get current playing track on android with xamarin android
get current playing track on android with xamarin android

Time:03-07

Is it possible to get the currently playing song on android using xamarin android?

I tried with the audiomanager but i couldn't read the current track from here

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

thanks in advance,

CodePudding user response:

When the phone is playing music, the system will send broadcast which contents the information about the music. So you can use the BroadcastReceiver to get it. I had done a simple demo to get the current playing track.

MainActivity.cs: Add the following code into the OnCreate method

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");
        
    }
}
  • Related