使用 AVFoundation 录制后无法播放视频
Cannot play video after record with 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];
中放置断点时。当程序遇到这个断点时,我继续 运行 它。它 运行 很好。
我不知道为什么。
任何帮助或建议将不胜感激。谢谢
查看Apple's documentation for the stopRecording method in AVCaptureFileOutput,我们可以看到有一个名为captureOutput:didFinishRecordingToOutputFileAtURL:fromConnections:error:
.
的AVCaptureFileOutputRecordingDelegate方法
看起来您正在尝试播放尚未完成写入磁盘的视频。磁盘写入是异步完成的,所以在执行下一行([player play]
)时,视频肯定不会写入。
我不知道你在哪里初始化你的 movieFileOutput
对象,但你应该设置它的委托(它可能是视图控制器)。
完成后,只需实施 captureOutput:didFinishRecordingToOutputFileAtURL:fromConnections:error:
方法(无论如何都是必需的),您可以将 [player play]
方法放在那里播放您的视频(在确认没有错误发生后)。
下面是该方法的示例实现:
- (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);
}
}
我解决了我的问题。
而不是在停止录制后调用 [self playVideo];
。我在 AVCaptureMovieFileOutput
的委托
上调用它
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error{
if ([error code] == noErr) {
[self playVideo];
}
}
我使用 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];
中放置断点时。当程序遇到这个断点时,我继续 运行 它。它 运行 很好。
我不知道为什么。
任何帮助或建议将不胜感激。谢谢
查看Apple's documentation for the stopRecording method in AVCaptureFileOutput,我们可以看到有一个名为captureOutput:didFinishRecordingToOutputFileAtURL:fromConnections:error:
.
看起来您正在尝试播放尚未完成写入磁盘的视频。磁盘写入是异步完成的,所以在执行下一行([player play]
)时,视频肯定不会写入。
我不知道你在哪里初始化你的 movieFileOutput
对象,但你应该设置它的委托(它可能是视图控制器)。
完成后,只需实施 captureOutput:didFinishRecordingToOutputFileAtURL:fromConnections:error:
方法(无论如何都是必需的),您可以将 [player play]
方法放在那里播放您的视频(在确认没有错误发生后)。
下面是该方法的示例实现:
- (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);
}
}
我解决了我的问题。
而不是在停止录制后调用 [self playVideo];
。我在 AVCaptureMovieFileOutput
的委托
- (void)captureOutput:(AVCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL fromConnections:(NSArray *)connections error:(NSError *)error{
if ([error code] == noErr) {
[self playVideo];
}
}