Android - mute microphone while recording video

Boldijar Paul picture Boldijar Paul · Dec 12, 2016 · Viewed 7.7k times · Source

I'm recording a video with the camera, using the MediaRecorder class, after following a tutorial similiar to this

http://androidcookbook.com/Recipe.seam;jsessionid=40151FCD26222877E151C3EEFB406EED?recipeId=1375&recipeFrom=ViewTOC

And I want while recording, to be able to mute / unmute the microphone. How's that possible?

I'm setting the audio source at start

 mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

But what if I want to record without sound at some point?

Answer

dogood picture dogood · Dec 21, 2016

Try the following code:

private void setMicMuted(boolean state){
    AudioManager myAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);

    // get the working mode and keep it
    int workingAudioMode = myAudioManager.getMode();

    myAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);

    // change mic state only if needed
    if (myAudioManager.isMicrophoneMute() != state) {
        myAudioManager.setMicrophoneMute(state);
    }

    // set back the original working mode
    myAudioManager.setMode(workingAudioMode);
}