1

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

//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 *)connections error:(NSError *)error

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

4

1 回答 1

0

您应该以“[self.session beginConfiguration]”开始您的“removeVideoInput:audioInput:”方法,并以“[self.session commitConfiguration]”结束它,就像添加音频输入一样。这让您对会话所做的所有更改都自动发生。

于 2014-03-20T23:18:00.783 回答