user3007870
user3007870

Reputation: 31

AVCaptureSession how to make mute input audio device?

That's how I trying to configure turn on/off mic volume with button :

//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;
 }

When I turn on/off mic it stops recording with error in :

  • (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error

In logs I see @"error: Recording Stopped".

Upvotes: 1

Views: 1915

Answers (1)

NES_4Life
NES_4Life

Reputation: 1055

You should start your 'removeVideoInput:audioInput:' method with '[self.session beginConfiguration]' and end it with '[self.session commitConfiguration]' as you did with the adding of the audio input. This lets all the changes you make on the session occur atomically.

Upvotes: 0

Related Questions