我需要能够同时工作AVCaptureVideoDataOutput
和AVCaptureMovieFileOutput
。以下代码有效,但是视频记录无效。在调用didFinishRecordingToOutputFileAtURL
之后,将直接调用startRecordingToOutputFileURL
委托(delegate)。现在,如果我从
只需注释掉以下行即可生成AVCaptureVideoDataOutput
:AVCaptureSession
视频录制有效,但是没有调用SampleBufferDelegate(我需要)。
我该如何使[captureSession addOutput:captureDataOutput];
和AVCaptureVideoDataOutput
同时工作。
- (void)initCapture {
AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput deviceInputWithDevice:[AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo] error:NULL];
captureDataOutput = [[AVCaptureVideoDataOutput alloc] init];
[captureDataOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
m_captureFileOutput = [[AVCaptureMovieFileOutput alloc] init];
NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];
[captureDataOutput setVideoSettings:videoSettings];
captureSession = [[AVCaptureSession alloc] init];
[captureSession addInput:captureInput];
[captureSession addOutput:m_captureFileOutput];
[captureSession addOutput:captureDataOutput];
[captureSession beginConfiguration];
[captureSession setSessionPreset:AVCaptureSessionPresetLow];
[captureSession commitConfiguration];
[self performSelector:@selector(startRecording) withObject:nil afterDelay:10.0];
[self performSelector:@selector(stopRecording) withObject:nil afterDelay:15.0];
[captureSession startRunning];
}
- (void) startRecording
{
[m_captureFileOutput startRecordingToOutputFileURL:[self tempFileURL] recordingDelegate:self];
}
- (void) stopRecording
{
if([m_captureFileOutput isRecording])
[m_captureFileOutput stopRecording];
}
- (NSURL *) tempFileURL
{
NSString *outputPath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"camera.mov"];
NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:outputPath]) {
[[NSFileManager defaultManager] removeItemAtPath:outputPath error:nil];
[outputPath release];
return [outputURL autorelease];
}
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didStartRecordingToOutputFileAtURL:(NSURL *)fileURL fromConnections:(NSArray *)connections
{
NSLog(@"start record video");
}
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error
{
NSLog(@"end record");
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
// do stuff with sampleBuffer
}
我应该补充,我得到了错误:
Error Domain=NSOSStatusErrorDomain Code=-12780 "The operation couldn’t be completed. (OSStatus error -12780.)" UserInfo=0x23fcd0 {AVErrorRecordingSuccessfullyFinishedKey=false}
从
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error
干杯
最佳答案
我已经联系了Apple支持的工程师,他告诉我不支持同时使用AVCaptureVideoDataOutput
+ AVCaptureMovieFileOutput
。我不知道他们将来是否会支持它,但是他使用了“目前不支持”这个词。
我鼓励您像我一样(bugreport.apple.com)填写有关此问题的错误报告/功能请求,因为它们可以衡量人们对某些东西的渴望程度,我们可能会在不久的将来看到这一点。