2

My goal is to detect when music starts playing on the device. In my case I want to launch volume controls on an Android Wear device but that's irrelevant for the question.

I know there is AudioManager.isMusicActive() but it requires polling. I would rather listen for a broadcast without keeping a service alive indefinitely.

The other alternative would be to listen for headphones being plugged in but apparently Intent.ACTION_HEADSET_PLUG is only delivered to dynamic receivers as this answer suggests.

I'm a bit clueless here. How can I listen for any audio related events without constant polling?

Community
  • 1
  • 1
Kirill Rakhman
  • 42,195
  • 18
  • 124
  • 148
  • Did you solve your problem? – Jasper Aug 12 '16 at 16:10
  • I just used a notification listener in the end. – Kirill Rakhman Aug 13 '16 at 12:04
  • Can you please explain it a bit more? I am trying to do something similar for a research project that logs data of how people use the sound of their phone (how much hours, what volume, with/without headphones). I was afraid that I had to check every X minutes, but if there is a way to avoid that it would be great! – Jasper Aug 15 '16 at 18:00
  • 1
    I use a `NotificationListenerService` and check if a notification of the music app, e.g. Google Play Music, is shown. – Kirill Rakhman Aug 15 '16 at 19:32
  • But not all (popular) music players create a notification when playing... so this is probably not working all the time. If I play a song in Spotify for example but keep the app open no notification is created so your notificationListener isn't working and you don't know if the music is playing or not... – Jasper Aug 16 '16 at 08:36
  • I know. I didn't find a better way. – Kirill Rakhman Aug 16 '16 at 08:38
  • Ok. Thanks, I probably won't either... – Jasper Aug 16 '16 at 08:41

0 Answers0