OGeek|极客世界-中国程序员成长平台

标题: ios - 录制的视频有时显示,有时不显示 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 12:38
标题: ios - 录制的视频有时显示,有时不显示

我有一个 View Controller ,用于记录用户的(正面)视频。使用如下代码:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = [paths objectAtIndex:0];
NSString *outputPath = [[NSString alloc] initWithFormat"%@", [basePath stringByAppendingPathComponent"output.mp4"]];
if ([[NSFileManager defaultManager] isDeletableFileAtPathutputPath])
    [[NSFileManager defaultManager] removeItemAtPathutputPath error:NULL];
NSURL *outputURL = [[NSURL alloc] initFileURLWithPathutputPath];
[movieFileOutput startRecordingToOutputFileURLutputURL recordingDelegate:self];
self.outputURL = outputURL;

其中 movieFileOutputAVCaptureMovieFileOutput 对象。我可以向您展示相机配置/设置代码(这不是图像选择器 Controller 或任何 Apple UI;它会在用户与完全不同的屏幕交互时在后台拍摄用户),但总的来说,我知道它可以工作。

然后,调用 [movieFileOutput stopRecording]; 并将用户导航到一个新屏幕,在该屏幕上播放刚刚发生的录制。我将 outputURL 对象传递给这个 newly initialized View Controller ,VC 使用以下代码播放它:

self.player = [[MPMoviePlayerController alloc] initWithContentURL:self.outputURL];
self.player.view.frame = CGRectMake(0, 0, self.view.frame.size.width, 320);
self.player.movieSourceType = MPMovieSourceTypeFile;
[[NSNotificationCenter defaultCenter] addObserver:self selectorselector(moviePlaybackComplete name:MPMoviePlayerPlaybackDidFinishNotification object:self.player];
self.player.controlStyle = MPMovieControlStyleNone;
self.player.shouldAutoplay = YES;
[self.player setFullscreen:YES animated:NO];
[self.player prepareToPlay];
[self.view addSubview:[self.player view]];
[self.player play];

所以,第一次,一切正常。应用程序录制视频,然后显示下一个屏幕,并显示视频。但是,如果您单击“放弃”按钮,它会一直回到应用程序的开头,并再次执行整个过程,它确实不起作用,而是播放器显示黑色屏幕。话虽如此,我打印出路径上的数据长度,它总是很大(因此该路径上存在某些东西)。

奇怪的是,我决定像这样打印 NSData 对象本身:

NSLog(@"%@", [NSData dataWithContentsOfURL:self.outputURL]);

确实打印了一个非常大的字符串(也许是字节?不确定)。 然而,由于某种原因,现在一切正常...当我添加此 NSLog() 时,该错误不再出现。当我删除它时,它再次发生!我已经严格对此进行了测试。

但是仍然有一些奇怪的时间包含这个 NSLog() 时,在播放了三遍黑屏之后。我很困惑为什么会出现这个错误,以及包含 NSLog() 究竟如何改变任何行为。

有什么想法吗?



Best Answer-推荐答案


我的猜测是你需要等待文件被保存。

AVCaptureFileOutput 的文档说:

When recording is stopped either by calling this method, by changing files using startRecordingToOutputFileURL:recordingDelegate:, or because of an error, the remaining data that needs to be included to the file will be written in the background. Therefore, before using the file, you must wait until the delegate that was specified in startRecordingToOutputFileURL:recordingDelegate: is notified when all data has been written to the file using the captureOutput:didFinishRecordingToOutputFileAtURL:fromConnections:error: method.

关于ios - 录制的视频有时显示,有时不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34253004/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4