AVCaptureSession如何制作静音输入音频设备?

时间:2013-11-19 08:56:01

标签: ios objective-c audio avcapturesession audio-capture

这就是我尝试使用按钮配置打开/关闭麦克风音量的方法:

//micBtn turn on/off
- (void)micTap:(UIButton*)sender
{
  if (sender.selected) {
    [self addAudioInput];
  } else {
    [self removeVideoInput:NO audioInput:YES];
  }
}

- (void)removeVideoInput:(BOOL)removeVI audioInput:(BOOL)removeAI
{
NSArray *inputs = self.session.inputs;

for (AVCaptureDeviceInput *input in inputs) {

    if (removeVI && [input.device hasMediaType:AVMediaTypeVideo]) {

        [self.session removeInput:input];
        continue;
    }

    if (removeAI && [input.device hasMediaType:AVMediaTypeAudio]) {

        [self.session removeInput:input];
    }
}

}


- (BOOL)addAudioInput //returns success of adding
{
NSArray *inputs = self.session.inputs;

BOOL alreadyHasAudioInput = NO;

for (AVCaptureDeviceInput *input in inputs) {
    if ([input.device hasMediaType:AVMediaTypeAudio]) {
        alreadyHasAudioInput = YES;

    }
}

if (alreadyHasAudioInput) {
    return NO;
}

AVCaptureDevice *audio = [AVCaptureDevice devices][kAudioType];

AVCaptureDeviceInput *audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audio error:nil];

[self.session beginConfiguration];

[self.session addInput:audioInput];

[self.session commitConfiguration];

return YES;
 }

当我打开/关闭麦克风时,它会在以下错误中停止录制:

  
      
  • (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL   fromConnections:(NSArray *)连接错误:(NSError *)错误
  •   

在日志中,我看到@“错误:录制已停止”。

1 个答案:

答案 0 :(得分:0)

您应该使用'[self.session beginConfiguration]'启动'removeVideoInput:audioInput:'方法,并使用'[self.session commitConfiguration]'结束,就像添加音频输入一样。这使您在会话中所做的所有更改都以原子方式进行。