How to mute and unmute microphone during a capture session in iOS

Viewed 223

I'm recoding a video in an iOS app using AVCaptureSession.

I want to mute the microphone during parts of the recording. I'm creating an AVCaptureConnection from my AVCaptureMovieFileOutput object.

    AVCaptureMovieFileOutput *movieFileOutput = [AVCaptureMovieFileOutput new];
    if([self.captureSession canAddOutput:movieFileOutput])
    {
        [self.captureSession addOutput:movieFileOutput];
        self.captureSession.sessionPreset = AVCaptureSessionPresetHigh;
        
        AVCaptureConnection *connection = [movieFileOutput connectionWithMediaType:AVMediaTypeVideo];
        if(connection.isVideoStabilizationSupported)
        {
            connection.preferredVideoStabilizationMode = AVCaptureVideoStabilizationModeAuto;
        }
        
        // AVCaptureConnection for audio
        self.audioConnection = [movieFileOutput connectionWithMediaType:AVMediaTypeAudio];

        self.movieFileOutput = movieFileOutput;
    }   

I can mute the microphone at the start of the recording with the enabled property.

self.audioConnection.enabled = NO;

However, if I enable this property during the recording, the capture session stops and this delegate method is called.

- (void)captureOutput:(AVCaptureFileOutput *)output 
didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL 
      fromConnections:(NSArray<AVCaptureConnection *> *)connections 
                error:(NSError *)error;

I can start recording a second video clip with audio and then combine the two video clips, but this solution is not ideal and creates a stutter between the two videos.

Is it possible to mute and unmute the microphone during a capture session in iOS?

0 Answers
Related