我已经设置了一个可以使用AVFoundation拍摄照片的会话,并且我有一个自定义相机,所以我没有使用默认的选择器控制器。如何添加录制视频选项?
在按钮上,我添加了一个手势,可以检测是否已经按住,这应该开始视频录制。我先发布用于图像捕获的代码,然后发布我尝试用于视频捕获的代码。很抱歉提前发帖
照片捕捉代码:
(查看确实加载)
session = [[AVCaptureSession alloc]init];
[session setSessionPreset:AVCaptureSessionPresetHigh];
inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError * error;
deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error];
if([session canAddInput:deviceInput])
{
[session addInput:deviceInput];
}
previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
CALayer * rootLayer = [myImageView layer];
[rootLayer setMasksToBounds:YES];
CGRect frame = CGRectMake(0,0,400,800);
[previewLayer setFrame:frame];
[rootLayer insertSublayer:previewLayer atIndex:0];
stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary * outputSettings = [[ NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey, nil];
[stillImageOutput setOutputSettings:outputSettings];
[session addOutput:stillImageOutput];
movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];
[session startRunning];
(按钮方式)
AVCaptureConnection * videoConnection = nil;
for(AVCaptureConnection * connection in stillImageOutput.connections)
{
for(AVCaptureInputPort * port in [connection inputPorts])
{
if([[port mediaType] isEqual:AVMediaTypeVideo])
{
videoConnection = connection;
break;
}
}
if(videoConnection)
{
break;
}
}
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
if(imageDataSampleBuffer != NULL)
{
NSData * imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage * image2 = [UIImage imageWithData:imageData];
myImageView.image= image2;
previewLayer.hidden = YES;
[self didFinishSelectingPhoto];
}
}];
视频捕获代码(不起作用)
- (void)doAction:(UILongPressGestureRecognizer *)recognizer {
if (recognizer.state == UIGestureRecognizerStateBegan) {
//start video
[session beginConfiguration];
AVCaptureDevice *videoCaptureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:videoCaptureDevice error:nil];
AVCaptureSession *session = [[AVCaptureSession alloc] init];
[session addInput:videoInput];
[session addOutput:movieFileOutput];
[session commitConfiguration];
[self startRecording];
}
}
- (void)startRecording
{
[movieFileOutput startRecordingToOutputFileURL:[self tempFileURL]
recordingDelegate:self];
}
- (void) stopRecording
{
NSLog(@"stop recording");
[movieFileOutput stopRecording];
}
- (NSURL *) tempFileURL
{
NSString *outputPath = [[NSString alloc] initWithFormat:@"%@%@", NSTemporaryDirectory(), @"output.mov"];
NSURL *outputURL = [[NSURL alloc] initFileURLWithPath:outputPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:outputPath]) {
NSLog(@"file saved");
}
return outputURL;
}