使用AVFoundation

时间:2016-02-03 02:16:36

标签: ios avfoundation

我使用AVFoundation录制视频,完成录制后此视频将自动运行。 这是录制视频的代码:

- (IBAction)captureVideo:(id)sender {
if (!isRecording) {
    isRecording = YES;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd-MM-yyyy||HH:mm:SS"];
    NSDate *now = [[NSDate alloc] init];
    NSString *theDate = [dateFormat stringFromDate:now];

    NSString *videoPath = [[NSString alloc] initWithString:[NSString stringWithFormat:@"%@/%@.mov",documentsDirectory,theDate]];
    outputURL = [NSURL fileURLWithPath:videoPath];
    [movieFileOutput startRecordingToOutputFileURL:outputURL recordingDelegate:self];
} else{
    isRecording = NO;
    [movieFileOutput stopRecording];

    [self playVideo];
}
}

播放视频:

AVPlayer *player = [AVPlayer playerWithURL:outputURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;

playerViewController.view.frame = CGRectMake(0, 150, self.view.frame.size.width, 400);
[self.view addSubview:playerViewController.view];
[player play];

奇怪的是,我无法在录制后播放视频,但是当我在第AVPlayer *player = [AVPlayer playerWithURL:outputURL];行放置断点时。当程序遇到这个断点时,我继续运行它。它运行正常。 我不知道为什么。 任何帮助或建议将不胜感激。感谢。

2 个答案:

答案 0 :(得分:0)

Looking at Apple's documentation for the stopRecording method in AVCaptureFileOutput, we can see that there is a AVCaptureFileOutputRecordingDelegate method called captureOutput:didFinishRecordingToOutputFileAtURL:fromConnections:error:.

So it looks like you're trying to play the video when it's not done writing to the disk. The disk writing is done asynchronously, so when the next line ([player play]) is being executed, the video most certainly won't be done writing.

I don't know where you initialized your movieFileOutput object, but you should set its delegate (it could be the view controller).

Once that's done, just implement the captureOutput:didFinishRecordingToOutputFileAtURL:fromConnections:error: method (which is required anyway) and you could put your [player play] method in there to play your video (after verifying that no error occured).

Here's an example implementation of that method:

- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error {

    if ([error code] == noErr) {
        [player play];
    } else {
        //Let the user know something went wrong
        NSLog(@"Error recording to output file: %@",error.localizedDescription);
    }

}

答案 1 :(得分:0)

I fixed my problem. Instead of call [self playVideo]; after stop recording. I call it on AVCaptureMovieFileOutput's delegate

- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error{
    if ([error code] == noErr) {
        [self playVideo];
    }
}