AVURLAsset cannot load with remote file

隐身守侯 提交于 2019-11-30 05:38:37

问题


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];
    ...
    }

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...

Regards, Quentin


回答1:


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];

If you need to have access to the AVURLAsset behind that you could follow these steps.

Step 1/ register for changes of the status property of the player item

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

Step 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;
        }
    }   
}

EDIT: corrected the answer



来源:https://stackoverflow.com/questions/4934886/avurlasset-cannot-load-with-remote-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!