我正在尝试使用自定义方案从服务器获取数据。 我设法获取原始数据,但我的委托实现似乎有问题,因为调用
//data is never nil
[loadingRequest.dataRequest respondWithData:data];
似乎永远不会回应AVPlayer。任何人都可以在这段代码中发现我错了。提前谢谢。
这是代码
NSURLComponents *comp = [[NSURLComponents alloc] initWithURL:[NSURL URLWithString: /*https*/item.contentUrl] resolvingAgainstBaseURL:NO];
comp.scheme = @"custom";
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:comp.URL options:nil];
AVAssetResourceLoader *loader = asset.resourceLoader;
[loader setDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithAsset:asset];
self.backupPlayer = [AVPlayer playerWithPlayerItem:playerItem];
这是我当前对委托方法的实现
- (BOOL) resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest
{
loadingRequest.contentInformationRequest.contentType = @"audio/mpeg";
loadingRequest.contentInformationRequest.contentLength = 200000;
loadingRequest.contentInformationRequest.byteRangeAccessSupported = YES;
self.loadingRequest = loadingRequest;
NSURLComponents *comp = [NSURLComponents componentsWithURL:loadingRequest.request.URL resolvingAgainstBaseURL:NO];
comp.scheme = @"https";
NSMutableURLRequest *rq = [loadingRequest.request mutableCopy];
[rq setURL:comp.URL];
NSLog(@"\nHeaders:\n%@", [rq allHTTPHeaderFields]);
if (self.session == nil)
{
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
self.session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]];
}
[[self.session dataTaskWithRequest:rq completionHandler:
^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
{
if (error != nil)
{
NSLog(@"Error, %@", error);
}
else
{
NSLog(@"Response: %@",response);
if (data != nil)
{
[self.loadingRequest.dataRequest respondWithData:data];
[self.loadingRequest finishLoading];
}
}
}]
resume];
return YES;
}