AVURLAsset无法加载远程文件 [英] AVURLAsset cannot load with remote file
问题描述
我在使用AVURLAsset时遇到问题。
I have a problem using AVURLAsset.
NSString * const kContentURL = @
"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8";
...
NSURL *contentURL = [NSURL URLWithString:kContentURL];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:contentURL
options:nil];
[asset loadValuesAsynchronouslyForKeys:[NSArray arrayWithObject:tracksKey]
completionHandler:^{
...
NSError *error = nil;
AVKeyValueStatus status = [asset statusOfValueForKey:tracksKey
error:&error];
...
}
在完成块中,状态为AVKeyValueStatusFailed并且错误消息是无法打开。我见过的所有例子,使用本地文件,所以使用远程文件可能有问题...
In the completion block, the status is AVKeyValueStatusFailed and the error message is "Cannot Open". All exemples I have seen, use a local file, so maybe there is a problem using a remote file...
问候,
Quentin
Regards, Quentin
推荐答案
您无法直接为HTTP Live流创建 AVURLAsset
,如下所述Apple的 AV基础编程指南。
您必须使用流网址创建 AVPlayerItem
并使用它实例化AVPlayer
You can't directly create an AVURLAsset
for an HTTP Live stream as stated in Apple's AV Foundation Programming Guide.
You'll have to create an AVPlayerItem
with the stream url and instantiate an AVPlayer with it
AVPlayerItem *pItem = [AVPlayerItem playerItemWithURL:theStreamURL];
AVPlayer *player = [AVPlayer playerWithPlayerItem:pItem];
如果您需要访问 AVURLAsset
您可以按照这些步骤进行操作。
If you need to have access to the AVURLAsset
behind that you could follow these steps.
步骤1 /注册更改播放器的状态
属性item
Step 1/ register for changes of the status
property of the player item
[playerItem addObserver:self forKeyPath:@"status" options:0 context:nil];
步骤2 / in observeValueForKeyPath:ofObject:change:context:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"status"]) {
AVPlayerItem *pItem = (AVPlayerItem *)object;
if (pItem.status == AVPlayerItemStatusReadyToPlay) {
// Here you can access to the player item's asset
// e.g.: self.asset = (AVURLAsset *)pItem.asset;
}
}
}
编辑:更正了答案
这篇关于AVURLAsset无法加载远程文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!