5

How do i check from which music app the music is getting played in android device?

Using AudioManager, we can check if the music is being played or no. But it doesn provide the details of the music app from which it is being played.

AudioManager am = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);

am.isMusicActive() returns true if music is played in device. But i want the music app name from which it is being played.

References :

How do you check if music is playing by using a broadcast receiver?

Android development : To find out which app is playing music currently

For instance, if i play music from Google Music player. Then i have to be able to get the music app name as "Google Music Player".

Any reply for this would be appreciated. Thanks!

Community
  • 1
  • 1

1 Answers1

0

Try this to get currently playing track's file path:

public Cursor query(Context context, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, int limit) {
    try {
        ContentResolver resolver = context.getContentResolver();
        if (resolver == null) return null;
        if (limit > 0) uri = uri.buildUpon().appendQueryParameter("limit", "" + limit).build();
        return resolver.query(uri, projection, selection, selectionArgs, sortOrder);
    } catch (UnsupportedOperationException ex) { return null; }
}
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String path = intent.getStringExtra("track").replaceAll("'","''");
        Cursor c = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                new String[] { MediaStore.Audio.Media.DATA }, MediaStore.Audio.Media.TITLE + "='" + path + "'",
                null, null, 0);
        try {
            if (c == null || c.getCount() == 0) return;
            int size = c.getCount();
            if (size != 1) return;
            c.moveToNext();

            // Here's the song path
            String songPath = c.getString(0);
            Log.d("ROAST_APP", "" + songPath);
        } finally {
            if (c != null) c.close();
        }
    }
};

@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");
    registerReceiver(mReceiver, iF);
}

sources:
Track info of currently playing music
Needletagger

Community
  • 1
  • 1
The Badak
  • 2,010
  • 2
  • 16
  • 28