【发布时间】:2015-11-10 19:12:02
【问题描述】:
这很奇怪,但基本上我在本地下载并保存了一个视频,并存储了url路径以提供给AVPlayer播放。
我第一次这样做时效果很好。我下载了一个文件,然后我可以尽情地播放它,直到我退出应用程序。当我第二次启动该应用程序时,当我尝试使用相同的确切路径播放相同的视频时,会出现黑屏。
因为我使用的是模拟器,所以我可以验证视频和图片确实仍然存在于我保存它们的同一个文件夹中,如果我从 Finder 中单击它们,我仍然可以播放它们。
也许这是一个缓存问题?如果重要,我在测试时将它们直接保存到Library 目录。
相关代码:
NSString *outputFile = [NSString stringWithFormat:@"video_%@.mp4", guid];
NSString *outputDirectory = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *tempPath = [outputDirectory stringByAppendingPathComponent:outputFile];
NSURL *fileURL = [NSURL fileURLWithPath:tempPath];
// save the video to the URL
然后我使用 NSString [fileURL path] “持久化”它(我构建它的方式,假设解决方案需要 NSString 到 NSURL 的转换)。
稍后我创建一个AVPlayerItem:
NSURL *url = [NSURL fileURLWithPath:persistedObject.contentURL];
NSLog(@"url: %@", url); // prints a valid location**
AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
** 例如,这是一个示例 url 位置
url: file:///Users/gabriel/Library/Developer/CoreSimulator/Devices/CE1FC933-808C-4003-9BE4-DEC59B787FF7/data/Containers/Data/Application/FAD072B4-B5B0-4487-8A76-57B047324A00/Library/picture_D8DEAFA5-0843-4AA3-BB32-C61E32D13579.mp4
有人建议我改用URLForDirectory:inDomain:appropriateForURL:create:error: 和URLByAppendingPathComponent:,我会研究一下。但是我仍然对为什么我第一次下载它时会播放它感到困惑,但当它是相同的文件时,应用程序退出后却不会播放。
【问题讨论】:
-
使用相关代码更新您的问题。请务必包含获取视频 URL 的相关代码。
-
完成,如果我遗漏了任何有用的信息,请告诉我
标签: ios objective-c video avfoundation avplayer