ios – AVAssetResourceLoaderDelegate – 仅请求前两个字节?

我正在实现一个AVAssetResourceLoaderDelegate,并且我在行为正确方面遇到了一些麻烦.我的目标是拦截AVPlayer发出的任何请求,自己发出请求,将数据写入文件,然后用文件数据响应AVPlayer.

我看到的问题:我可以拦截一个请求,它只需要两个字节,然后响应它.在那之后,我没有得到任何更多请求命中我的AVAssetResourceLoaderDelegate.

当我从AVPlayer拦截一个AVAssetResourceLoadingRequest时,它看起来像这样:

<AVAssetResourceLoadingRequest: 0x17ff9e40,URL request = <NSMutableuRLRequest: 0x17f445a0> { URL: fakeHttp://blah.com/blah/blah.mp3 },request ID = 1,content @R_765_4045@ion request = <AVAssetResourceLoadingContent@R_765_4045@ionRequest: 0x17ff9f30,content type = "(null)",content length = 0,byte range access supported = NO,disk caching permitted = NO>,data request = <AVAssetResourceLoadingDataRequest: 0x17e0d220,requested offset = 0,requested length = 2,current offset = 0>>

如您所见,这只是对前两个字节数据的请求.我正在URL中使用fakeHttp协议,只用http替换它,并自己发出请求.

然后,这是我有一些数据后如何响应请求:

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

     //Make the remote URL request here if needed,omitted

    CFStringRef contentType = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType,(__bridge CFStringRef)([self.response MIMEType]),NULL);
    loadingRequest.content@R_765_404[email protected]terangeAccessSupported = YES;
    loadingRequest.content@R_765_404[email protected] = CFBridgingrelease(contentType);
    loadingRequest.content@R_765_404[email protected] = [self.response expectedContentLength];

    //Where responseData is the appropriate NSData to respond with
    [loadingRequest.dataRequest respondWithData:responseData];

    [loadingRequest finishLoading];
    return YES;
}

我已逐步完成此操作并验证content@R_765_4045@ionRequest中的所有内容都已正确填写,并且我发送的数据是具有适当长度的NSData(在本例中为两个字节).

没有更多请求被发送给我的代表,并且播放器不播放(大概是因为它只有两个字节的数据,并且还没有再请求).

有没有人有这方面的经验指出我可能做错事的地方?我正在运行iOS 7.

编辑:在我调用finishedLoading之后,这是我完成的请求的样子:

<AVAssetResourceLoadingRequest: 0x16785680,URL request = <NSMutableuRLRequest: 0x166f4e90> { URL: fakeHttp://blah.com/blah/blah.mp3  },content @R_765_4045@ion request = <AVAssetResourceLoadingContent@R_765_4045@ionRequest: 0x1788ee20,content type = "public.mp3",content length = 7695463,byte range access supported = YES,data request = <AVAssetResourceLoadingDataRequest: 0x1788ee60,current offset = 2>>

解决方法

- (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest
{    
    loadingRequest.content@R_765_404[email protected] = @"public.aac-audio";
    loadingRequest.content@R_765_404[email protected] = [self.fileData length];
    loadingRequest.content@R_765_404[email protected]terangeAccessSupported = YES;

    NSData *requestedData = [self.fileData subdataWithRange:NSMakeRange((NSUInteger)loadingRequest.dataRequest.requestedOffset,(NSUInteger)loadingRequest.dataRequest.requestedLength)];
    [loadingRequest.dataRequest respondWithData:requestedData];
    [loadingRequest finishLoading];

    return YES;
}

这个实现对我有用.它总是要求前两个字节,然后是整个数据.如果你没有得到另一个回调,则表示你所做的第一个回复出现了问题.我想问题是你使用的是MIME内容类型而不是UTI.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...