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

一个人想着一个人 提交于 2019-12-09 09:20:37

问题


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

来源:https://stackoverflow.com/questions/29260067/how-to-release-the-microphone-resource-when-another-android-app-needs-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!