AVCaptureSession用音频录制视频 [英] AVCaptureSession Record Video With Audio
问题描述
我已设置我的应用程序,以使用AVCaptureSession记录来自摄像机的视频,但是其中没有音频。我需要做什么来记录音频,然后将其添加到文件的videoOutput中?这是我录制视频的代码:
I have my app set up to record video from the camera using an AVCaptureSession, however, there is no audio with it. What do I need to do to record audio and then add it to the videoOutput for the file? Here is my code for recording the video:
AVCaptureSession *session = [[AVCaptureSession alloc] init];
[session beginConfiguration];
session.sessionPreset = AVCaptureSessionPresetMedium;
CALayer *viewLayer = self.vImagePreview.layer;
NSLog(@"viewLayer = %@", viewLayer);
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
captureVideoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
captureVideoPreviewLayer.frame = self.vImagePreview.bounds;
[self.vImagePreview.layer addSublayer:captureVideoPreviewLayer];
AVCaptureDevice *device = [self frontFacingCameraIfAvailable];
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
if (!input) {
// Handle the error appropriately.
NSLog(@"ERROR: trying to open camera: %@", error);
}
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
AVCaptureMovieFileOutput *movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
NSString *archives = [documentsDirectoryPath stringByAppendingPathComponent:@"archives"];
NSString *outputpathofmovie = [[archives stringByAppendingPathComponent:@"Test"] stringByAppendingString:@".mp4"];
NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputpathofmovie];
[session addInput:input];
[session addOutput:movieFileOutput];
[session commitConfiguration];
[session startRunning];
[movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];
我为音频添加了另一个输入,但是它不能与后台的mpmovieplayercontroller一起使用。是否有任何想法可以播放一个视频并同时从摄像机录制音频和视频?
I added another input for the audio, but it wont work with the mpmovieplayercontroller that is in the background. Are there any thoughts to something that could play one video and simultaneously record audio and video from a camera?
推荐答案
您尚未包括音频设备:
AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
AVCaptureDeviceInput * audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:nil];
[session addInput:audioInput]
介于 beginConfiguration $之间c $ c>和
commitConfiguration
。会起作用的!!!
between beginConfiguration
and commitConfiguration
. It'll work!!!
这篇关于AVCaptureSession用音频录制视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!