2

I'm trying to use a custom scheme to fetch data from a server. I manage to get the raw data, but there seems to be something wrong with my delegate implementation, because the call

//data is never nil
[loadingRequest.dataRequest respondWithData:data];

never seems to respond back to the AVPlayer. Can anyone spot what I'm wrong in this code. Thanks in advance.

Here's the code

NSURLComponents *comp = [[NSURLComponents alloc] initWithURL:[NSURL URLWithString: /*https*/item.contentUrl] resolvingAgainstBaseURL:NO];
comp.scheme = @"custom";
AVURLAsset      *asset          = [[AVURLAsset alloc] initWithURL:comp.URL options:nil];
AVAssetResourceLoader *loader   = asset.resourceLoader;
[loader setDelegate:self queue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)];
AVPlayerItem    *playerItem     = [AVPlayerItem playerItemWithAsset:asset];

self.backupPlayer = [AVPlayer playerWithPlayerItem:playerItem];

This is my current implementation of the delegate method

- (BOOL) resourceLoader:(AVAssetResourceLoader *)resourceLoader shouldWaitForLoadingOfRequestedResource:(AVAssetResourceLoadingRequest *)loadingRequest
{


    loadingRequest.contentInformationRequest.contentType = @"audio/mpeg";
    loadingRequest.contentInformationRequest.contentLength = 200000;
    loadingRequest.contentInformationRequest.byteRangeAccessSupported = YES;

    self.loadingRequest = loadingRequest;

    NSURLComponents *comp = [NSURLComponents componentsWithURL:loadingRequest.request.URL resolvingAgainstBaseURL:NO];
    comp.scheme = @"https";
    NSMutableURLRequest *rq = [loadingRequest.request mutableCopy];
    [rq setURL:comp.URL];
    NSLog(@"\nHeaders:\n%@", [rq allHTTPHeaderFields]);
    if (self.session == nil)
    {
        NSURLSessionConfiguration *config = [NSURLSessionConfiguration     defaultSessionConfiguration];
        self.session = [NSURLSession sessionWithConfiguration:config delegate:self     delegateQueue:[NSOperationQueue mainQueue]];
    }
    [[self.session dataTaskWithRequest:rq completionHandler:

  ^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error)
      {
         if (error != nil)
         {
              NSLog(@"Error, %@", error);
          }
          else
          {
              NSLog(@"Response: %@",response);
              if (data != nil)
              {
                  [self.loadingRequest.dataRequest respondWithData:data];
                  [self.loadingRequest finishLoading];
              }

          }

      }]
     resume];


    return YES;
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Did you end up getting this working? – Mof Sep 29 '16 at 01:29
  • yeb needed to implement a custom ServerTrust as I was using a self signed certificate. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler – Þorvaldur Rúnarsson Sep 29 '16 at 12:17

0 Answers0