32

TL;DR: My app is hogging the user's microphone. Can I turn it off automatically whenever another app needs to use the mic?

I have an Android app that has some really cool microphone functionality, similar to Amazon Alexa, that stays on all the time in a background service. The problem is, my app hogs the users' microphone, making it unusable:

enter image description here

However, this is terrible application behavior on my behalf, and I want to do my best to avoid it. Is it possible to be notified when another application requests to use the microphone, so that I can automatically stop my service?

PS: I am using the Pocketsphinx library for continuous background voice recognition.

4
  • 5
    Instead implement a button in your app. Button press recording start, button up recording stop and send the voice clip to pocketsphinx.
    – g10dras
    Commented Jun 8, 2018 at 23:30
  • 2
    Android P will prevent idle applications from listening to the microphone (source: theverge.com/2018/3/7/17091104/…), so you might need to re-think your design anyway (perhaps using @g10dras's suggestion of push-to-activate). Commented Aug 9, 2018 at 6:11
  • stackoverflow.com/questions/42185088/… This will work for sure!!
    – BHARATHWAJ
    Commented Aug 9, 2018 at 12:09
  • 1
    Why not use the hey google interface? Commented Nov 21, 2019 at 19:21

3 Answers 3

1

This is tricky, as I'm not a ware of any API for this. This surely will require system-level APIs to work like an "Ok Google" type of thing.

A viable option would be (from https://stackoverflow.com/a/43623308/603270) to run a Job at regular intervals, checking for foreground apps using android.permission.PACKAGE_USAGE_STATS.

This might suffice. But you could also add things regarding phone calls (how to detect phone call broadcast receiver in android) using android.intent.action.PHONE_STATE or media playback (via Receiver or maybe even MediaPlayer directly).

1

If you're really wanting to get this thing working, an alternative would be to get an array list of all installed apps on the system and which ones require permission to use the mic or not, then use an accessibility service to monitor the users screen if an app the user just opened requires the mic (which you'll know from the array you just grabbed). From there, disable the mic in your app if their app needs the mic. The background service can then check in intervals of, say, two minutes, to see if the app that required the mic is still open. This is really inefficient. But if you don't care, then this might be a good option.

0

There is no standard way to inform another app that you want access to the microphone (so that they release the resources and let you access it). You could however send a broadcast to all other apps ("requesting the microphone"), but the other apps would have to implement this feature (and very few or zero developers will do this).

I would recommend you to simply inform the user that the microphone is currently not available, because you can't do anything else.

1
  • And speaking as one of those other developers who want the mic - what are we supposed to do when another app asks for it? I work on voip apps, so if we've got the mic, you're on a phone call, and I don't want to give it up. Unless maybe you've muted the call, or some other set of things that turn out to be a nasty hairball of special cases that virtually no one cares about and are expensive to test. Commented Sep 6, 2018 at 23:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.