Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get currently-playing-song information?

Background

I want to see if I can get information about the currently playing song, to be able to show more information about it, or just the information I get instead.

I've wanted to check it out because I've noticed there is a setting (here) on a popular music player app called "Musicolet", of "Broadcast album art". Sure it says just "album art", but it also says it's for "3rd party". So I wanted to investigate if it's possible.

The problem

Not sure if it's possible or not. I see conflicting answers about it.

What I've found

I've found some interesting links about it:

  1. I've also found a question on StackOverflow (here) about getting it, but it is missing some crucial code.
  2. On the other hand, I've found another post saying that it's not official and that you probably can't do it, here.
  3. I've noticed that Spotify had (or still have?) something like that (here, except I can't see album art there), but it's a bit old.
  4. I've also found an interesting interface RemoteControlClient.OnMetadataUpdateListener (from here), but couldn't find any sample or tutorial about it, except perhaps this one one from XDA.

Of course, I tried using the tips I've found from those links, but failed to get anything to really work.

So maybe it was not possible in the past, and then it was possible. But then for some reason I don't see a sample/tutorial of how to use it, including on the docs.

I think that it if's possible, it requires notification access, because it's about the same as reading the currently playing music from the notification of the music player.

The questions

  1. Is it possible to register to a callback (and also query whenever you wish) of when a song plays, and get information about it ?

    Meaning song-title, album-name, file-path/uri, album-art...

  2. If it's possible, how? What does it require?

  3. If it's not possible, is there any workaround? Anything that works for some apps? Maybe something that exists in the notifications, more officially, that I can detect that it's of this kind, and fetch this information?

like image 395
android developer Avatar asked Oct 23 '25 18:10

android developer


1 Answers

You need to have notification listener service, third party apps cannot access media sessions otherwise.

Declare service somewhere:

import android.service.notification.NotificationListenerService

class NotiService : NotificationListenerService()

Add it to manifest:

<service android:name=".service.NotiService"
    android:label="Enable media controls"
    android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
    <intent-filter>
        <action android:name="android.service.notification.NotificationListenerService" />
    </intent-filter>
</service>

Then you can pass it as an argument to getActiveMediaSessions inside Activity:

val m = getSystemService<MediaSessionManager>()!!
val component = ComponentName(this, NotiService::class.java)
val sessions = m.getActiveSessions(component)
Log.d("Sessions", "count: ${sessions.size}")
sessions.forEach {
    Log.d("Sessions", "$it -- " + (it?.metadata?.keySet()?.joinToString()))
    Log.d("Sessions", "$it -- " + (it?.metadata?.getString(MediaMetadata.METADATA_KEY_TITLE)))
}

Now navigate to System Settings -> Notification Access and enable it for NotiService and it should run without issues.

like image 85
Pawel Avatar answered Oct 26 '25 06:10

Pawel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!