Ios AVAssetResourceLoaderDelegate不回叫AVplayer
我正在尝试设置一个渐进的视频下载,从服务器URL流。我能够调用AVAssetResourceLoaderDelegate,首先是信息请求,然后是数据请求,它获取前1000字节的数据。但是,代理从不回电话并启动播放机。我逐字节检查了数据,结果是正确的Ios AVAssetResourceLoaderDelegate不回叫AVplayer,ios,objective-c,avplayer,Ios,Objective C,Avplayer,我正在尝试设置一个渐进的视频下载,从服务器URL流。我能够调用AVAssetResourceLoaderDelegate,首先是信息请求,然后是数据请求,它获取前1000字节的数据。但是,代理从不回电话并启动播放机。我逐字节检查了数据,结果是正确的 -(void)videoCallingMethod { NSURL *url = [NSURL URLWithString:@"fakeurl://mypath.com/moviefile.mp4"]; dispatch_queue
-(void)videoCallingMethod {
NSURL *url = [NSURL URLWithString:@"fakeurl://mypath.com/moviefile.mp4"];
dispatch_queue_t serialQueue = dispatch_queue_create("com.meels.queue", DISPATCH_QUEUE_SERIAL);
dispatch_async(serialQueue, ^{
_asset = [[AVURLAsset alloc] initWithURL:url options:nil];
[_asset.resourceLoader setDelegate:self queue:serialQueue];
_playerItem = [AVPlayerItem playerItemWithAsset:_asset];
_player = [AVPlayer playerWithPlayerItem:_playerItem]; // This will call the delegate
_playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
[_playerLayer setFrame:self.view.frame];
[self.view.layer addSublayer:_playerLayer];
[_player play];
});
}
- (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest {
BOOL returnValue = FALSE;
if(loadingRequest.contentInformationRequest) {
loadingRequest.contentInformationRequest.contentType = @"public.mpeg-4"; // "public.mpeg-4" "kUTTypeMPEG4"
loadingRequest.contentInformationRequest.contentLength = 1000;
loadingRequest.contentInformationRequest.byteRangeAccessSupported = YES;
NSLog(@"contentInformationRequest loadingRequest %@", loadingRequest);
[loadingRequest finishLoading]; // This will trigger the data request
returnValue = TRUE;
} else if(loadingRequest.dataRequest) {
NSLog(@"It's a dataRequest");
// Here the url custom scheme is removed and the url changed back to http to point to the exact url location where my file is
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:loadingRequest.request.URL resolvingAgainstBaseURL:NO];
urlComponents.scheme = @"http";
NSMutableURLRequest *mutableLoadingRequest = [loadingRequest.request mutableCopy];
[mutableLoadingRequest setURL:urlComponents.URL];
NSURLSession *session = [[NSURLSession alloc] init];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[NSOperationQueue mainQueue]];
[[session dataTaskWithRequest:mutableLoadingRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"Session block complete with error %@", error);
if (error != nil) {
NSLog(@"Error, %@", error);
} else {
NSLog(@"Response: %@",response); // status code: 206
NSLog(@"data: %@",data); // The data is correct
if (data != nil) {
NSLog(@"Completing the request: %@", loadingRequest.dataRequest);
[loadingRequest.dataRequest respondWithData:data];
[loadingRequest finishLoading];
}
}
}] resume];
returnValue = TRUE;
}
return returnValue;
}
当我调用[LoadingRequestFinishLoading]时
我希望代理将数据传递回资源加载程序
,播放器开始播放,但这不会发生
委托已完成,没有任何错误。我检查了数据,结果是正确的。服务器以状态代码206作出响应。我试着以不同的格式使用MIME和UTI。它不影响响应。此外,如果禁用委托并恢复http url方案,视频将加载并播放良好。因此,文件本身没有问题