How to check if music is playing from a music app?

How can I check from which music app music is playing on my Android device?

Using AudioManager, we can check whether music is playing or not. But it does not provide details of the music application from which it is being played.

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

am.isMusicActive() returns true if music is playing on the device. But I want the name of the music app to play.

References:

How to check if music is playing with the broadcast receiver?

Android development: find out which application is currently playing

For example, if I play music on a Google Music player. Then I should be able to get the name of the music application as "Google Music Player".

Any answer for this would be appreciated. Thanks!

+5
source share
1 answer

Try this to get the path to the track file at the moment:

 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 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 current music information
Needletagger

0
source

All Articles