简体   繁体   English

Objective-C:在 iOS 上使用 AVFoundation 录制视频时静音/取消静音

[英]Objective-C: To mute/unmute audio while recording video using AVFoundation on iOS

I am using the sample provided by Apple at this link to record and save video recording.我正在使用 Apple 在此链接上提供的示例来录制和保存视频记录。

Wanted the ability to mute and un-mute audio before recording the video.希望能够在录制视频之前将音频静音和取消静音。

On Objective-C, I tried the belowmentioned code to mute/unmute on button click before starting video recording.在 Objective-C 上,我尝试了下面提到的代码在开始视频录制之前在按钮单击时静音/取消静音。 But the video is getting recorded with the audio.但是视频正在与音频一起录制。

Tried without calling the beginConfiguration and commitConfiguration on the session object but still issue exists.尝试不调用会话对象上的 beginConfiguration 和 commitConfiguration 但问题仍然存在。

Any idea how to handle the same in Objective-C ?知道如何在 Objective-C 中处理相同的问题吗?

- (IBAction)muteAudio:(id)sender
{

    self.muteAudio = !self.muteAudio;

    NSError *error = nil;

    [self.session beginConfiguration];

    if(self.muteAudio == FALSE)
    {

        // Add audio input.
        AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
        AVCaptureDeviceInput *audioDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:&error];
        if ( ! audioDeviceInput ) {
            NSLog( @"Could not create audio device input: %@", error );
        }
        if ( [self.session canAddInput:audioDeviceInput] ) {
            [self.session addInput:audioDeviceInput];
        }
        else {
            NSLog( @"Could not add audio device input to the session" );
        }
    }
    else
    {

        // Add audio input.
        AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
        AVCaptureDeviceInput *audioDeviceInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:&error];
        if ( ! audioDeviceInput ) {
            NSLog( @"Could not create audio device input: %@", error );
        }
        [self.session removeInput:audioDeviceInput];


    }
    [self.session commitConfiguration];
}

Found the solution.找到了解决办法。 Add the below-mentioned code in the toggleMovieRecording method which will get called when you hit the record button.在 toggleMovieRecording 方法中添加下面提到的代码,当您点击录制按钮时,该方法将被调用。

    AVCaptureConnection *audioConnection = [self.movieFileOutput connectionWithMediaType:AVMediaTypeAudio];
    audioConnection.enabled = !self.muteAudio;

The method after adding the logic to disable/enable audio.添加逻辑以禁用/启用音频后的方法。

- (IBAction)toggleMovieRecording:(id)sender
{
    /*
        Disable the Camera button until recording finishes, and disable
        the Record button until recording starts or finishes.

        See the AVCaptureFileOutputRecordingDelegate methods.
     */
    self.cameraButton.enabled = NO;
    self.recordButton.enabled = NO;
    self.captureModeControl.enabled = NO;

    /*
        Retrieve the video preview layer's video orientation on the main queue
        before entering the session queue. We do this to ensure UI elements are
        accessed on the main thread and session configuration is done on the session queue.
    */
    AVCaptureVideoOrientation videoPreviewLayerVideoOrientation = self.previewView.videoPreviewLayer.connection.videoOrientation;

    dispatch_async( self.sessionQueue, ^{
        if ( ! self.movieFileOutput.isRecording ) {
            if ( [UIDevice currentDevice].isMultitaskingSupported ) {
                /*
                    Setup background task.
                    This is needed because the -[captureOutput:didFinishRecordingToOutputFileAtURL:fromConnections:error:]
                    callback is not received until AVCam returns to the foreground unless you request background execution time.
                    This also ensures that there will be time to write the file to the photo library when AVCam is backgrounded.
                    To conclude this background execution, -[endBackgroundTask:] is called in
                    -[captureOutput:didFinishRecordingToOutputFileAtURL:fromConnections:error:] after the recorded file has been saved.
                */
                self.backgroundRecordingID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];
            }

            // Update the orientation on the movie file output video connection before starting recording.
            AVCaptureConnection *movieFileOutputConnection = [self.movieFileOutput connectionWithMediaType:AVMediaTypeVideo];
            movieFileOutputConnection.videoOrientation = videoPreviewLayerVideoOrientation;


            //Code to enable and disable audio in the recorded video file.
            AVCaptureConnection *audioConnection = [self.movieFileOutput connectionWithMediaType:AVMediaTypeAudio];
            audioConnection.enabled = !self.muteAudio;


            // Start recording to a temporary file.
            NSString *outputFileName = [NSUUID UUID].UUIDString;
            NSString *outputFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[outputFileName stringByAppendingPathExtension:@"mov"]];
            [self.movieFileOutput startRecordingToOutputFileURL:[NSURL fileURLWithPath:outputFilePath] recordingDelegate:self];
        }
        else {
            [self.movieFileOutput stopRecording];
        }
    } );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM