Forcing MediaRecorder to use internal microphone

流过昼夜 提交于 2019-12-19 03:56:55

问题


I'm using something like this to initiate audio recording:

MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile("/dev/null");
recorder.prepare();
recorder.start();

I want to always record from the internal microphone. Using the code above, the audio source switches to the external microphone as soon as one is plugged in. I've looked over the docs and can't find a way to set a preferred mic.


回答1:


Unfortunately, you're trying to go against Android's audio management, this is, the system selects input device depending on selected audio source and, when the headset is pluged in, input, for MIC audio source, is switched to the headset. You can confirm this checking logcat's AudioPolicyManager's related messages.

My suggestion would be playing with different audio sources to trick the system. I would recommend using CAMCORDER or, at least, try VOICE_RECOGNITION or VOICE_COMMUNICATION.

Main problem with CAMCORDER is that it can be using multimedia microphone, this is, the one at the back of the device. More in detail, I would suggest following procedure:

  1. Listen for ACTION_HEADSET_PLUG Intent.
  2. When you receive the intent, try to switch audio source to CAMCORDER.
  3. CAMCORDER audio source is selected depending on active camera so you can try to force front camera usage (and, then, front internal mic) through MediaREcorder's setCamera

Hope this helps



来源:https://stackoverflow.com/questions/19815850/forcing-mediarecorder-to-use-internal-microphone

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