Bypass MIC exclusivity using NDK ?

1,025 views
Skip to first unread message

Hammad Saleem

unread,
Oct 14, 2013, 9:02:19 AM10/14/13
to andro...@googlegroups.com
Hi, 
As you are aware the standard Android API for recording audio gives exclusive access to the mic to any client… this means that if one app has the mic open then the second app requesting the mic will fail to get it. I was wondering if its possible to use the NDK to bypass this exclusivity requirement.. The app I want to create must not interfere with other audio apps such as VoIP or voice commands … Any help/pointers would be appreciated. 

Thanks!

Baodong Chen

unread,
Oct 14, 2013, 11:06:32 PM10/14/13
to andro...@googlegroups.com
just like camera,i think this is android's design, only one app can focus at all time, so devices like camera or MIC
are exclusive.some time this is good but not always!!


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to android-ndk...@googlegroups.com.
To post to this group, send email to andro...@googlegroups.com.
Visit this group at http://groups.google.com/group/android-ndk.
For more options, visit https://groups.google.com/groups/opt_out.

Brendon Costa

unread,
Oct 15, 2013, 12:05:29 AM10/15/13
to andro...@googlegroups.com
Also from my experience there is a bug in the NDK, where no error is reported when opening the mic and it is already open, it just fails to produce audio callbacks. I mentioned this on the list earlier but have not yet had a chance to raise a ticket about it.



Athos Bacchiocchi

unread,
Oct 15, 2013, 8:49:53 AM10/15/13
to andro...@googlegroups.com
You may release the audio focus (and "stop" opensl input) by yourself when you decide you don't need it (in the onPause/onStop, or when your window loses focus), and/or implement the OnAudioFocusChangeListener to release the audio input driver when other apps ask for audio focus. You must use the java api for this, though. Have a look here: http://developer.android.com/training/managing-audio/audio-focus.html

athos

Brendon Costa

unread,
Oct 15, 2013, 3:01:57 PM10/15/13
to andro...@googlegroups.com
Yes. Though some apps like Skype don't manage audio focus correctly and will take audio focus but not relinquish the mic when another app steals audio focus from them permanently or temporarily. Because managing audio focus correctly is "optional" it means you cant be sure things will work correctly when used alongside other apps.

I believe this failure to implement audio focus correctly was done in Skype because of a bug in older versions of Android (fixed after 4.0.4), where calling AudioManager.SetMode() would make you loose audio focus to the system if you had it. So Skype, which also calls SetMode(), just ignores audio focus loss and keeps the mic open. As soon as one app does that it breaks the whole focus model.

My work-around for this was to add a watchdog to monitor the audio thread, where if I don't get an audio tick within some time frame (500msec when using 20msec blocks) I emit my own error and close things down. Its an imperfect solution but works for now.

Hammad Saleem

unread,
Oct 15, 2013, 8:51:17 PM10/15/13
to andro...@googlegroups.com
Thanks everyone! Was not aware that Audio Focus was the way to arbitrate MIC access. Getting notified when another app requests the MIC is exactly what i need. Will see how well this works in practice though.

The watchdog approach is another model I had considered, but the behavior seems to be inconsistent across the 4-5 devices I have tested and will make it hard to use consistently.

Hammad Saleem

unread,
Oct 31, 2013, 11:30:50 AM10/31/13
to andro...@googlegroups.com
As it turns out Audio Focus only applies to Audio-Out i.e playback and not Audio-In i.e Mic.

The app not only loses Audio Focus when any app tries to play a sound, but more importantly we also don't get notified when an app asks for the MIC without actually playing a sound.... 

So this solution unfortunately didn't work...

Any more ideas ?

I would just like to find a way where my app (that needs to have the MIC ON) can co-exist with other apps that also need the mic i.e Voice search, video recorder, and VoIP apps. Even if that means I need to give up the MIC when others need it.

Thanks for any help.

Glenn Kasten

unread,
Nov 1, 2013, 2:23:49 PM11/1/13
to andro...@googlegroups.com
Issue is tracked at
"Allow a way to arbitrate MIC access between multiple apps needing the MIC"
Reply all
Reply to author
Forward
0 new messages