我有一个AVCaptureSession,负责管理我的应用程序的视频和图像捕获。 我在应用程序中也有声音效果,当然当用户沉默时 在他们的iPhone上切换静音开关。
然而,当我向会话添加AVCaptureAudioDataOutput时,设置静音开关时声音效果不再被静音。
以下是用于添加audion的代码:
NSError *errorAud;
AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
AVCaptureDeviceInput *audioDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:&errorAud];
if (errorAud) {
NSLog(@"%@", [errorAud localizedDescription]);
}
if ( [avSession canAddInput:audioDeviceInput] ) {
[avSession addInput:audioDeviceInput];
[self setAudioCaptureDeviceInput:audioDeviceInput];
}
audioOutput = [[AVCaptureAudioDataOutput alloc] init];
[audioOutput setSampleBufferDelegate:self queue:vsessionQueue];
if ([avSession canAddOutput:audioOutput]) {
[avSession addOutput:audioOutput];
}
for (AVCaptureConnection *connection in [audioOutput connections]) {
for (AVCaptureInputPort *port in [connection inputPorts]) {
if ([[port mediaType] isEqual:AVMediaTypeAudio]) {
audioConnection = connection;
break;
}
}
}
AVAuthorizationStatus audioAuthorizationStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
//This sort of fixes the silent mode issue but makes the capture session
//stop outputs stop calling didOutputSampleBuffer
//Whenever a sound effect is played
//avSession.usesApplicationAudioSession = NO;
//avSession.automaticallyConfiguresApplicationAudioSession = NO;
从上面可以看出,我尝试搞乱 usesApplicationAudioSession 和 automaticConfiguresApplicationAudioSession ,但设置 usesApplicationAudioSession = NO 使capturesession停止调用
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)连接
每当我使用AVAudioPlayer播放音效时