这就是我尝试使用按钮配置打开/关闭麦克风音量的方式:
//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
在日志中我看到@“错误:记录已停止”。