我已经设法让AVPlayer与AVAssetResourceLoaderDelegate一起在iOS 7上播放m4a文件,但不能在iOS 6上执行。
这个
- (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader
shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest使用以下请求调用委托方法:
2014-05-12 15:14:58.798 AVPlayer-Delegate[661:1103] Requested data: {
Range = "bytes=0-1";
"X-Playback-Session-Id" = "5B64BE4E-442A-4A37-9263-04D22CDBCB28";
}我将根据请求返回两个第一个字节,但是再也不会调用委托方法。我试图在我要传递的响应对象中提供各种标题,但是没有帮助。
这就是我的实现:
- (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader
shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest
{
NSLog(@"Requested data: %@",
loadingRequest.request.allHTTPHeaderFields);
NSDictionary *headers = @{@"Content-Type": @"audio/x-m4a",
@"Accept-Ranges" : @"bytes",
@"Content-Length" : [NSString stringWithFormat:@"%d", 2],
@"Content-Range" : [NSString stringWithFormat:@"bytes 0-1/%d", self.fileData.length],
@"X-Playback-Session-Id" : loadingRequest.request.allHTTPHeaderFields[@"X-Playback-Session-Id"],
@"ETag" : @"TAG"};
NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:loadingRequest.request.URL statusCode:206 HTTPVersion:@"HTTP/1.1" headerFields:headers];
//NSURLResponse *response = [[NSURLResponse alloc] initWithURL:loadingRequest.request.URL MIMEType:@"audio/m4a" expectedContentLength:self.fileData.length textEncodingName:@"UTF-8"];
NSData *requestedData = [self.fileData subdataWithRange:NSMakeRange(0, 2)];
[loadingRequest finishLoadingWithResponse:response data:requestedData redirect:nil];
return YES;
}我真的很感谢你的帮助。
谢谢。
发布于 2014-05-17 05:15:23
AVAssetResourceLoaderDelegate在IOS 6和IOS 7上的工作方式不同。
AVAssetResourceLoaderDelegate协议现在支持从媒体资源加载任意范围的字节。
标准的预IOS7 7解决方案是使用本地HTTP服务器。参见例如https://stackoverflow.com/a/21225985
https://stackoverflow.com/questions/23612094
复制相似问题