Objective-C:在 iOS 上使用 AVFoundation 录制视频时静音/取消静音
Posted
技术标签:
【中文标题】Objective-C:在 iOS 上使用 AVFoundation 录制视频时静音/取消静音【英文标题】:Objective-C: To mute/unmute audio while recording video using AVFoundation on iOS 【发布时间】:2018-03-02 10:19:42 【问题描述】:我正在使用 Apple 在此 link 提供的示例来录制和保存视频录制。
希望能够在录制视频之前将音频静音和取消静音。
在 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];
【问题讨论】:
【参考方案1】:找到了解决办法。在 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];
);
【讨论】:
以上是关于Objective-C:在 iOS 上使用 AVFoundation 录制视频时静音/取消静音的主要内容,如果未能解决你的问题,请参考以下文章
如何在 iOS 上使用 Objective-C 触发按需 ***
在 iOS 上使用 Parse BaaS:我应该使用 PFFile 来保存原始类型和 Objective-C 类吗?
在 Objective-C 中的 iOS 6 上强制横向方向