【问题标题】:AVURLAsset cannot load with remote fileAVURLAsset 无法加载远程文件
【发布时间】:2011-02-08 15:22:42
【问题描述】:

我在使用 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,错误消息为“无法打开”。我见过的所有例子,都使用本地文件,所以使用远程文件可能有问题......

问候, 昆汀

【问题讨论】:

    标签: iphone ios avfoundation


    【解决方案1】:

    您不能像 Apple 的 AV Foundation Programming Guide 中所述直接为 HTTP 直播流创建 AVURLAsset。 您必须使用流 url 创建一个 AVPlayerItem 并用它实例化一个 AVPlayer

    AVPlayerItem *pItem = [AVPlayerItem playerItemWithURL:theStreamURL];
    AVPlayer *player = [AVPlayer playerWithPlayerItem:pItem];
    

    如果您需要访问后面的AVURLAsset,您可以按照以下步骤操作。

    第 1 步/注册播放器项目的 status 属性的更改

    [playerItem addObserver:self forKeyPath:@"status" options:0 context:nil];
    

    第 2 步/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;
            }
        }   
    }
    

    编辑:更正答案

    【讨论】:

    • 谢谢,我尝试了这个解决方案,但是没有调用 currentItem 值的观察者方法,因为在我添加观察者时已经设置了属性。所以我在 AVPlayerItem 的状态上放置了一个观察者方法,从这里我可以得到 AVPlayer currentItem。
    • @Quentin 你说得对,谢谢!与我的代码的另一部分混合......我已经编辑了我的答案以更正它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 2014-06-26
    • 1970-01-01
    相关资源
    最近更新 更多