2

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.

nmh
  • 2,497
  • 1
  • 15
  • 27
Michal Pietras
  • 442
  • 5
  • 16
  • Did you ever find a solution to this? I'm seeing the same behavior (only first two bytes requested) on an iOS 7 device. There are no requests after the initial request for the first two bytes. – Alexander Jul 14 '14 at 18:24

1 Answers1

1

AVAssetResourceLoaderDelegate works differently on IOS 6 and IOS 7. See What's New in IOS 7.0

The AVAssetResourceLoaderDelegate protocol now supports loading of arbitrary ranges of bytes from a media resource.

The standard pre-IOS7 workaround is to use a local HTTP server. See for example https://stackoverflow.com/a/21225985

Community
  • 1
  • 1
  • So what's the purpose of AVAssetResourceLoaderDelegate protocol on iOS 6 if it cannot be used for loading arbitrary ranges of bytes? Are there any other possible usage scenarios? Thanks. – Michal Pietras May 21 '14 at 12:09