How to release the microphone resource when another Android app needs it?
Asked Answered
C

1

10

I have implemented an Android background service (based in Pocket Sphinx) that continuously listens, detects and spots certain keywords when the phone's screen is on, and then reacts accordingly (for a domotic project). However, my app should always have a lower priority and I want it to release the microphone's resources when any other app needs to use it. Is there any system intent or system broadcast that I can receive when an app -wants- to use the microphone (so that my service can release it)? Or, is there a better approach or better practice?

This is the log with the errors I want to avoid when another app wants to access the microphone:

AudioFocus  requestAudioFocus() from android.media.AudioManager@419c3908com.google.android.voicesearch.a.h@419c67b8
I/MicrophoneInputStream(32360): mic_starting com.google.android.speech.audio.w@41989a30
D/AudioHardwareALSA(277): openInputStream: devices 0x400000 format 0x1 channels 16 sampleRate 16000
E/AudioHardwareALSA(277): error:Input stream already opened for recording
E/AudioRecord(32360): Could not get audio input for record source 6
E/AudioRecord-JNI(32360): Error creating AudioRecord instance: initialization check failed.
E/AudioRecord-Java(32360): [ android.media.AudioRecord ] Error code -20 when initializing native AudioRecord object.
E/MicrophoneInputStream(32360): Failed to initialize AudioRecord
Calculator answered 25/3, 2015 at 15:36 Comment(5)
You may get good info at this link: android-developers.blogspot.in/2013/08/…Eduardo
It seems such a thing is not possible in android, so my workaround was a dirty solution: obtain a list of the installed apps, filter them to those apps who have the RECORD permission... and then run a background service that detects which app is in the foreground, if such app is in the List of Apps that use the RECORD permission, bingo, I send a broadcast o my app, telling it to release the microphone. If you need the code tell me. In theory this is a bad practice, but Google's practice is worse as long as they don't provide an appropiate solution.Calculator
Part 2: code.google.com/p/android/issues/…Calculator
Hi @Josh, your solution sounds great. We have exactly the same requirement. Have you made your code public by any chance?Rustproof
Hi @Josh, facing the same requirement. Have you made your code public ?Solidstate
G
0

Check this answer of mine: https://mcmap.net/q/1169483/-in-my-android-how-to-stop-audio-recording-when-other-app-wants-record-audio. If you are a privileged system app (CAPTURE_AUDIO_HOTWORD permission needed), you can use the HOTWORD audio source, which does exactly this - gives in the microphone to any other thing requesting it using the MIC source, I imagine.

If not, below Android 10 I don't think it's possible in a decent way (or at least I didn't find any actual solutions). As of 10, no idea because I didn't study the documentation on it yet xD, but I think it's different(?): https://developer.android.com/guide/topics/media/sharing-audio-input.

Gangland answered 3/3, 2023 at 12:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.