3

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

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

Here is the code used for adding 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 with usesApplicationAudioSession and automaticallyConfiguresApplicationAudioSession but setting usesApplicationAudioSession = NO makes the capturesession stop calling

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

whenever I play a sound effect using AVAudioPlayer

Zigglzworth
  • 6,645
  • 9
  • 68
  • 107

0 Answers0