簡體   English   中英

Objective-C:在 iOS 上使用 AVFoundation 錄制視頻時靜音/取消靜音

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

我正在使用 Apple 在此鏈接上提供的示例來錄制和保存視頻記錄。

希望能夠在錄制視頻之前將音頻靜音和取消靜音。

在 Objective-C 上,我嘗試了下面提到的代碼在開始視頻錄制之前在按鈕單擊時靜音/取消靜音。 但是視頻正在與音頻一起錄制。

嘗試不調用會話對象上的 beginConfiguration 和 commitConfiguration 但問題仍然存在。

知道如何在 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];
}

找到了解決辦法。 在 toggleMovieRecording 方法中添加下面提到的代碼,當您點擊錄制按鈕時,該方法將被調用。

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

添加邏輯以禁用/啟用音頻后的方法。

- (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