AVAssetResourceLoaderDelegate not being called

2019-06-27 04:38发布

问题:

I've been trying to get some messages back on the AVAssetResourceLoaderDelegate protocol but it never seems to get called. I've verified that everything is happening on the main thread; from creation of AVURLAsset, creation of the AVPlayerItem, creation of the delegate, and the delegate queue is set to the main thread. I'm trying to stream web-hosted MP4 content and unencrypted HLS content.

My declarations:

@property (readwrite, strong) AVPlayer* player;
@property (strong) AVPlayerItem* playerItem;
@property (strong) id<AVAssetResourceLoaderDelegate> resourceLoaderDelegate;

Code for playing the video:

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
...
self->_resourceLoaderDelegate = [[MyAssetResourceLoaderDelegate alloc] init];
[asset.resourceLoader setDelegate:self->_resourceLoaderDelegate
                            queue:dispatch_get_main_queue()];
...
self->_playerItem = [AVPlayerItem playerItemWithAsset:asset];
...
[self setPlayer:[AVPlayer playerWithPlayerItem:self->_playerItem]];

However, resourceLoader:shouldWaitForLoadingOfRequestedResource: never gets called (neither do any of the other delegate methods).

For clarity's sake, I'm testing using the iOS Simulator as well as an iPhone 5s running iOS 8 (and the results appears identical).

I've verified that all of the code is being executed on the main thread, as I read in this question that everything must be on the same thread.

Does anyone have any suggestions, or perhaps a reference to some available source code where this delegate actually gets called?

Update: I've determined that this code works fine when the url is to a local file, but it still doesn't work when it's to a remote file. Is that expected behavior or is there a way I can do this for remote files?

回答1:

AVAssetResourceLoaderDelegate works only when we use a "Non Standard/Non Reserved" url scheme. HTTP, HTTPS etc are considered reserved URL schemes and iOS will not make a delegate call if the url has one of those schemes. What I ended up doing was using my_own_http for the http urls and my_own_https for the https urls. It works well. One more thing to remember, if you have the URL with non standard scheme, it may not work on other devices which support HLS.