I have managed to get AVPlayer
working with AVAssetResourceLoaderDelegate
to play m4a file on iOS 7
but I cannot do it on iOS 6
.
The
- (BOOL)resourceLoader:(AVAssetResourceLoader *)resourceLoader
shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest
delegate method is getting called with following request:
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";
}
I am returning the two first bytes as requested but the delegate method is never called again. I was trying to provide various headers in the response object I am passing but it didn't help.
This is my implementation:
- (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;
}
I would really appreciate any help with it.
Thanks.