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

前端之家收集整理的这篇文章主要介绍了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 information request = <AVAssetResourceLoadingContentInformationRequest: 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.contentInformationRequest.byteRangeAccessSupported = YES;
    loadingRequest.contentInformationRequest.contentType = CFBridgingRelease(contentType);
    loadingRequest.contentInformationRequest.contentLength = [self.response expectedContentLength];

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

    [loadingRequest finishLoading];
    return YES;
}

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

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

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

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

<AVAssetResourceLoadingRequest: 0x16785680,URL request = <NSMutableURLRequest: 0x166f4e90> { URL: fakeHttp://blah.com/blah/blah.mp3  },content information request = <AVAssetResourceLoadingContentInformationRequest: 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.contentInformationRequest.contentType = @"public.aac-audio";
    loadingRequest.contentInformationRequest.contentLength = [self.fileData length];
    loadingRequest.contentInformationRequest.byteRangeAccessSupported = 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.

猜你在找的iOS相关文章