【发布时间】:2016-10-29 10:38:15
【问题描述】:
我只是在这上面浪费了大约 3 个小时,但我看不出哪里出错了。
我正在尝试播放我使用 AVPlayer 在本地存储的视频。这就是我启动播放器的方式:
- (void)openVideo:(NSURL *)videoURL {
NSLog(@"Playing video with the url:\n%@", videoURL);
AVPlayer *player = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = player;
[self presentViewController:playerViewController animated:YES completion:nil];
}
这是NSURL我正在传递:
+ (NSURL *)getURL:(NSString *)itemKey withType:(NSString *)itemType {
NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *dataFilePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: [NSString stringWithFormat:@"%@.%@", itemKey, itemType]]];
NSURL *itemURL;
if ([[NSFileManager defaultManager] fileExistsAtPath: dataFilePath]){ // if data exists
itemURL = [NSURL fileURLWithPath:dataFilePath];
}
else {
NSLog(@"The data requested does not exist! Returning an empty url file...");
itemURL = nil;
}
return itemURL;
}
当我运行openVideo 时,我得到的输出是:
Playing video with the url:
/var/mobile/Containers/Data/Application/72C35DC4-9EF1-4924-91F4-EDA4BDB6AAD3/Documents/sample.vid
但我不断收到禁用的视频播放器..
我做错了什么?
【问题讨论】:
-
是您尝试播放的本地文件,名称为
sample.vid,还是您只是将其重命名为您放入此问题的代码?我认为 AVPlayer 可能会使用扩展名(例如mov、mp4等)来确定视频的编码。 -
@MichaelDautermann 该文件在此处重命名,但它确实以
vid结尾。我尝试将其重命名为mp4,但这也不起作用。 -
不要给它扩展名
.vid。你确定这是一个.mp4文件吗?您可以使用 Mac 上的 QuickTime 播放器和/或设备上的移动 Safari 打开该文件吗? -
是的,文件没问题(我也尝试了一堆其他文件,同样的问题)。我会尝试完全删除扩展名,但为什么 AVPlayer 会从
NSURL获取文件的编码呢? -
@MichaelDautermann 尝试更改扩展名并将其全部删除,但仍然没有。尝试使用在线视频的 URL 是可行的,因此它必须是导致问题的本地文件的 URL。