使用AVFoundation录制视频

时间:2014-12-29 16:03:55

标签: ios objective-c iphone avfoundation avcapturesession

我创建了会话,设备,输入和输出但是当我调用 startRecordingToOutputFileURL:recordingDelegate:方法时,它调用 didFinishRecordingToOutputFileAtURL 委托方法,所以它完成了开始!

这是我的代码..

-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

AVCaptureSession *session = [[AVCaptureSession alloc] init];
[session setSessionPreset:AVCaptureSessionPresetPhoto];

AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];


AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error];

if ([session canAddInput:deviceInput]) {
[session addInput:deviceInput];
}

AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];

[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
CALayer *rootLayer = [[self view] layer];
[rootLayer setMasksToBounds:YES];
CGRect frame = self.view.frame;

[previewLayer setFrame:frame];

[rootLayer insertSublayer:previewLayer atIndex:0];

_movieFileOutput = [[AVCaptureMovieFileOutput alloc] init];

if ([session canAddOutput:movieFileOutput]) {
    [session addOutput:movieFileOutput];
}

[session startRunning];
}
-(void)buttonClicked {
[_movieFileOutput startRecordingToOutputFileURL:url recordingDelegate:self];
}

- (void)captureOutput:(AVCaptureFileOutput *)captureOutput
didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL
      fromConnections:(NSArray *)connections
            error:(NSError *)error {
    //This method is getting called when I press the button
    NSLog(@"Finished Recording");

}

我的代码出了什么问题? 我错过了什么吗?

由于

1 个答案:

答案 0 :(得分:1)

最后它奏效了!

问题是网址。 我已经尝试了很多不使用它们的URL,它必须在NSDocumentsDirectory中,这就是它!