IOS / iPhone - Silent switch not working after adding AVCaptureAudioDataOutput to AVCaptureSession

I have an AVCaptureSession that manages the video and image capture for my application. I also have sound effects in the app, which are of course disabled when the user toggles the silence switch on their iPhone.

However, when I add AVCaptureAudioDataOutput to the session, the sound effects are no longer blocked when the silence switch is set.

Here is the code used to add 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; 

      

As you can from the above, I tried messing around with useApplicationAudioSession and automaticallyConfiguresApplicationAudioSession , but setting useApplicationAudioSession = NO makes the capture call termination

- (void) captureOutput: (AVCaptureOutput *) captureOutput didOutputSampleBuffer: (CMSampleBufferRef) sampleBuffer fromConnection: (AVCaptureConnection *) connection

whenever i play a sound effect with AVAudioPlayer

+3


source to share





All Articles