我使用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];
。当程序遇到这个断点时,我继续运行它。它运行良好。 我不知道为什么。 任何帮助或建议,将不胜感激。谢谢。
我想你应该记录到缓冲区/高速缓存或其他临时位置,而不是正确的文件时被存储。它会让你的视频有机会获得记录,任何操作系统或处理框架都有机会在保存到文件之前完成其工作。断点指向它停止的那个方向,它给它一个写完文件的机会。 – noobsmcgoobs
查看此链接https://www.objc.io/issues/23-video/capturing-video/特别是有关'AVCaptureDataOutput'和'AVAssetWriter'的章节 – noobsmcgoobs