iOS 6上的AVAssetResourceLoaderDelegate协议

时间:2014-05-12 14:43:44

标签: ios objective-c ios6 avplayer

我设法让AVPlayerAVAssetResourceLoaderDelegate合作,在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;

}

我真的很感激任何帮助。

感谢。

1 个答案:

答案 0 :(得分:1)

AVAssetResourceLoaderDelegate在IOS 6和IOS 7上的工作方式不同。请参阅What's New in IOS 7.0

  

AVAssetResourceLoaderDelegate协议现在支持加载   来自媒体资源的任意字节范围。

标准的IOS7之前的解决方法是使用本地HTTP服务器。请参阅示例https://stackoverflow.com/a/21225985