Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AVAssetResourceLoader delegate not set

I've been trying to implement AVAssetResourceLoaderDelegate but so far have been unable to successfully set the delegate, causing my delegate code to never get called.

Consider this:

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
[asset.resourceLoader setDelegate:[[MyAssetResourceLoaderDelegateCode alloc] init]
                            queue:queue];
NSLog(@"Delegate: %@", asset.resourceLoader.delegate);

This code always prints Delegate: (null). How do I set the delegate when the delegate refuses to be set? What am I missing?

This occurs on the simulator and on an iPhone 5s running iOS 8.

like image 481
MxSagan Avatar asked Jul 25 '26 04:07

MxSagan


1 Answers

The delegate you associate with this object must adopt the AVAssetResourceLoaderDelegate protocol. So the code should be like this:

@interface YourClass () <AVAssetResourceLoaderDelegate>
@end

@implementation YourClass

- (void)someMethod
{
    NSURL *url = [NSURL URLWithString:@"...."];
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
    [asset.resourceLoader setDelegate:self queue:queue];
}

#pragma mark - AVAssetResourceLoaderDelegate methods

- (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest
{

}

@end

OR

if you have separate delegate MyAssetResourceLoaderDelegateCode somewhere then you have to keep the strong reference to the object MyAssetResourceLoaderDelegateCode you created and pass it as a delegate. When you do [asset.resourceLoader setDelegate:... The resourceLoader is just have a weak reference to the object you made. So, the code can be:

@property (nonatomic, strong) MyAssetResourceLoaderDelegateCode *assetObject;

self.assetObject = [MyAssetResourceLoaderDelegateCode new];

[asset.resourceLoader setDelegate:self.assetObject ...];
like image 104
kabarga Avatar answered Jul 27 '26 17:07

kabarga