我将我的应用设置为使用AVCaptureSession从相机录制视频,但是没有音频。如何录制音频然后将其添加到文件的videoOutput中我需要做什么?以下是录制视频的代码:
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。是否有任何想法可以播放一个视频并同时录制来自相机的音频和视频?
答案 0 :(得分:30)
您尚未加入音频设备:
AVCaptureDevice *audioDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeAudio];
AVCaptureDeviceInput * audioInput = [AVCaptureDeviceInput deviceInputWithDevice:audioDevice error:nil];
[session addInput:audioInput]
介于beginConfiguration
和commitConfiguration
之间。它会工作!!!
答案 1 :(得分:9)
在beginConfiguration()
和commitConfiguration()
// Add audio device to the recording
let audioDevice = AVCaptureDevice.defaultDeviceWithMediaType(AVMediaTypeAudio)
do {
let audioInput = try AVCaptureDeviceInput(device: audioDevice)
self.captureSession.addInput(audioInput)
} catch {
print("Unable to add audio device to the recording.")
}
答案 2 :(得分:0)
在 swift 5x 中,你可以使用这个:
do {
guard let audioDevice = AVCaptureDevice.default(for: AVMediaType.audio) else {
print("Default audio device is unavailable.")
setupResult = .configurationFailed
session.commitConfiguration()
return
}
// Add audio input
let audioInput = try AVCaptureDeviceInput(device: audioDevice)
if session.canAddInput(audioInput) {
session.addInput(audioInput)
} else {
print("Couldn't add audio device input to the session.")
setupResult = .configurationFailed
session.commitConfiguration()
return
}
} catch {
print("Couldn't create Audio device input: \(error)")
setupResult = .configurationFailed
session.commitConfiguration()
return
}