Search code examples
androidmicrophoneandroid-audiomanager

Android mute microphone while not in call


I want to mute a microphone for quite some time, say a day. However, when a user makes a call or somebody calls, I want to enable the microphone and disable it afterwards. I did this:

AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 
audioManager.setMicrophoneMute(true);

This code works except in a situation after a call. After the call, the microphone is enabled. I suspect that a phone app can enable microphone again and so can other apps. How can I make microphone muted after the call? And is there a way to restrict other apps from enabling microphone?


Solution

  • Obliviously the Phone App enables the Microphone before answer/plac a call, but it doesn't restore the previous state because the normal state for the Mic is to be Enabled.

    You can use two different approaches: (1) set a Listener that detect voicecall end and restore the previous state or (2) if your device has Root Permissions and the Mic is present in "dev/inout/eventX" (where "X" is a progressive number but it is different between devices and even between reboots) you could open it in Exclusive mode, in this way no-one could open/use it again until you release the "lock". However an App (the Phone App or WhatsApp when the recording button is pressed) could crash if it doesn't manage very well this case.

    However I would notice you that the "AudioManager.setMicrophoneMute()" is implementation dependant and could not work on all Android versions (it depends of the Minifacturer).