我在此link使用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];
}
答案 0 :(得分: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];
}
} );
}