Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use AVAssetResourceLoader?

The documentation says about AVURLAsset.resourceLoader:

During loading, the resource loader object may be asked to assist in the loading of a resource. For example, a resource that requires decryption might result in the resource loader being asked to provide the appropriate decryption keys. You can assign a delegate object to the resource loader object and use your delegate to intercept these requests and provide an appropriate response.

So what I'm trying to do in my code is:

NSURL* url = ...;
_asset = [[AVURLAsset alloc] initWithURL:url options:nil];

AVAssetResourceLoader* loader = _asset.resourceLoader;
[loader setDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];

_playerItem = [AVPlayerItem  playerItemWithAsset:_asset];
_player = [AVPlayer playerWithPlayerItem:_playerItem];

// ... setting up the player layer

[_player play];

and also in my class I add AVAssetResourceLoaderDelegate into @interface and also implement method:

- (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest
{
    NSLog(@"YES");
    return YES;
}

Whatever URL is passed, this method is never get called. Doesn't metter if the URL is valid media over HTTP, or using my custom registered NSURLProtocol.

When does the resourceLoader suppose to call its delegate?

like image 734
Dmitry Avatar asked Aug 13 '13 09:08

Dmitry


1 Answers

You should try use a custom URL scheme, for instance: instead of http://myserver.com/listen.m3u use my_custom_scheme://myserver.com/listen.m3u. By default HTTP & HTTPS is handled by Apple so the delegate won't be called. To bypass this, you need to use a custom scheme to force the call for the delegate methods.

like image 103
Bogdan Avatar answered Nov 06 '22 20:11

Bogdan