在Android 6.0.1或更高版本上使用意图捕获视频时静音麦克风

时间:2017-12-02 11:01:55

标签: android android-intent android-camera android-video-record

我想在Android 6.0上捕获一个视频,并希望它在捕获视频时不会录制音频。我尝试以编程方式静音麦克风,但它无法在Android 6.0及更高版本上运行。有人请分享您的经验来解决这个问题任何类型的帮助将不胜感激! 我使用下面的代码来静音麦克风

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

使用意图打开相机

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 个答案:

答案 0 :(得分:0)

在通话模式中可以提供帮助

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

答案 1 :(得分:0)

试试这个

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

}