Search code examples
androidaudio-recordingmediarecordervideo-recording

Android - mute microphone while recording video


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?


Solution

  • 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);
    }