我正在尝试实现一个功能,您可以在 AVCaptureSession 期间启用或禁用麦克风。这就是我启用设备的方式(初始化)
-(void)initMicrophoneInput:(AVCaptureSession*)session{
AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
_microphoneInput = [[AVCaptureDeviceInput alloc] initWithDevice:audioDevice error:nil];
if ([session canAddInput:_microphoneInput]){
[session addInput:_microphoneInput];
} else {
NSLog(@"can't add input");
}
}
这是我禁用/启用麦克风的功能:
- (IBAction)manageMicrophone:(id)sender {
[_captureSession beginConfiguration];
if(_microphoneMuted == NO){
[_captureSession removeInput:_microphoneInput];
}else{
[self initMicrophoneInput:_captureSession];
}
[_captureSession commitConfiguration];
_microphoneMuted = !_microphoneMuted;
}
初始化和移除麦克风效果很好,但是我无法再次启用麦克风。我的 _captureSession.inputs 包含“微型”设备,但我不再从- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection;