Mute microphone while capturing video using intent on Android 6.0.1 or above

1.2k Views Asked by At

I want to capture a video on android 6.0 and also want that it will not record audio while capturing video. I tried to mute microphone programmatically but it is not working on Android 6.0 and above. Someone please share your experience to solve this problem any type of help will be appreciated! I used below code to mute microphone

AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
    audioManager.setMode(AudioManager.ADJUST_MUTE);
        if (audioManager.isMicrophoneMute() == false) {
            audioManager.setMicrophoneMute(true);
        } else {
            audioManager.setMicrophoneMute(false);
        }

and for open camera using intent

Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

        fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);

        // set video quality
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
        intent.putExtra(MediaStore.MEDIA_SCANNER_VOLUME, 0);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file

        // start the video capture Intent
        startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
2

There are 2 best solutions below

1
On

It can help while put mode in call

 AudioManager audioManager = ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE));
            audioManager.setMode(AudioManager.MODE_NORMAL);
            if(state) //state-boolean
            {
               audioManager.setMicrophoneMute(false);
               audioManager.setMode(AudioManager.MODE_IN_CALL);
               state = false;
            }
            else
            {
              audioManager.setMicrophoneMute(true);
              state = true;
            }
0
On

Try this

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

}