Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I stop hogging the microphone

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.

like image 762
Ruchir Baronia Avatar asked Jun 08 '18 02:06

Ruchir Baronia


2 Answers

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).

like image 83
shkschneider Avatar answered Nov 18 '22 15:11

shkschneider


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.

like image 1
AntonioSanchez Avatar answered Nov 18 '22 16:11

AntonioSanchez