ios AVURLAsset 拒绝加载视频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4101380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AVURLAsset refuses to load video
提问by Noah Witherspoon
I'm trying to load a video file into my iPad app as an AVURLAsset
, using the asynchronous-loading stuff to wait for it to be ready. Problem is, when I run it, I get a completely generic "failure" error message that I have no idea what to do with. The video works if I hand it to an MPMoviePlayerController
, but AVURLAsset
seems to refuse to have anything to do with it.
我正在尝试将视频文件作为AVURLAsset
. 问题是,当我运行它时,我收到一条完全通用的“失败”错误消息,我不知道该怎么办。如果我把它交给一个MPMoviePlayerController
,视频就可以工作,但AVURLAsset
似乎拒绝与它有任何关系。
Code:
代码:
asset = [[AVURLAsset alloc] initWithURL:[NSURL URLWithString:[docPath stringByAppendingPathComponent:@"video.mov"]] options:nil];
[asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:@"tracks"] completionHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
[self composeIfReady];
});
}];
...
...
- (void)composeIfReady
{
NSError *error = nil;
if([asset statusOfValueForKey:@"tracks" error:&error] == AVKeyValueStatusFailed)
NSLog(@"error loading: %@", [error description]);
if(error == nil)
NSLog(@"okay awesome");
}
The output:
输出:
error loading: Error Domain=AVFoundationErrorDomain Code=-11800 "The operation couldn't be completed. (AVFoundationErrorDomain error -11800.)" UserInfo=0x1696f0 {NSUnderlyingError=0x169a40 "The operation couldn't be completed. (OSStatus error -12936.)"}
-11800, by the way, is the error code for "unknown error". Kind of a dead end. Any ideas? Is there something I should be setting up before I try to load the asset?
顺便说一下,-11800 是“未知错误”的错误代码。有点死胡同。有任何想法吗?在我尝试加载资产之前,我应该设置什么吗?
回答by Noah Witherspoon
Solved. The trick: use fileURLWithPath:
, not URLWithString:
. Apparently the difference is really, really significant.
解决了。诀窍:使用fileURLWithPath:
,而不是URLWithString:
。显然,差异非常非常显着。
回答by Edwin Iskandar
If anyone is still having issues with this even after using fileURLWithPath, try requesting NSTimeIntervals in the time array if you are using int(s).
如果有人在使用 fileURLWithPath 后仍有问题,请尝试在时间数组中请求 NSTimeIntervals(如果您使用的是 int(s))。
This does not work:
这不起作用:
NSMutableArray *playbackTimes = [NSMutableArray array];
for (int i = 0; i <= 10; i ++) {
[playbackTimes addObject:@(i)];
}
[self.videoPlayer requestThumbnailImagesAtTimes:playbackTimes timeOption:MPMovieTimeOptionNearestKeyFrame];
This works:
这有效:
NSMutableArray *playbackTimes = [NSMutableArray array];
for (int i = 0; i <= 10; i ++) {
[playbackTimes addObject:@((NSTimeInterval)i)];
}
[self.videoPlayer requestThumbnailImagesAtTimes:playbackTimes timeOption:MPMovieTimeOptionNearestKeyFrame];
回答by naituw
If you are using -[NSURL fileURLWithPath:]
to create the URL and still get the same error.
如果您正在使用-[NSURL fileURLWithPath:]
创建 URL 并且仍然收到相同的错误。
Check the AVURLAssetReferenceRestrictionsKey
,
Your local m3u8 may failed to play if it contains remote resource.
检查一下AVURLAssetReferenceRestrictionsKey
,如果你的本地m3u8包含远程资源,可能无法播放。
Set the value to @(AVAssetReferenceRestrictionForbidNone)
should solve the problem.
将值设置为@(AVAssetReferenceRestrictionForbidNone)
应该可以解决问题。