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.
Not sure if it's possible or not. I see conflicting answers about it.
I've found some interesting links about it:
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.
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...
If it's possible, how? What does it require?
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With