1
votes

Is there a way to stream audio file from Google Drive with AVPlayer?

I have tried with both file.downloadUrl and file.webContentLink and it is not working.

Code:

GTLDriveFile *file = [self.data objectAtIndex:indexPath.row];
if (player)
{
    [player removeObserver:self forKeyPath:@"status"];
    [player pause];
}

player = [AVPlayer playerWithURL:[NSURL URLWithString:file.downloadUrl]];
//or
//player = [AVPlayer playerWithURL:[NSURL URLWithString:file.webContentLink]];
[player addObserver:self forKeyPath:@"status" options:0 context:nil];

if (delegate && [delegate respondsToSelector:@selector(audioPlayerDidStartBuffering)])
[delegate audioPlayerDidStartBuffering];

If it is not possible to stream, is it possible to start download in /tmp folder and play while downloading?

2
can u plz tell me ur problem is resolved regarding streaming audio file from google drive. - ChenSmile
Nope, it is not resolved - vburojevic
@Wikiboo...i too have same issue not able to resolve but if we discuss it..i think we can solve it...what player are u using for streaming predefined MpMovieplayer of AVPlayer - ChenSmile

2 Answers

2
votes

I could solve it just by appending the access_token to the download url

audiofile.strPath=[NSString stringWithFormat@"%@&access_token=%@",downloadUrl,accessToken];   

pass the strPath to your AvPlayer object.

you can fetch the access token from the GTMOAuth2Authentication object

Note that you might need to refresh it if its expires.

Hope this helps you.

Regards Nitesh

1
votes

That is simply because you didn't provide your client's access code from header of the download request. When you get downloadUrl, that link is not public link and you should provide same authorization as you did for all other Drive API requests.

For example, Object-c code for downloading content from downloadUrl would be like this:

+ (void)downloadFileContentWithService:(GTLServiceDrive *)service
                                  file:(GTLDriveFile *)file
                       completionBlock:(void (^)(NSData *, NSError *))completionBlock {
  if (file.downloadUrl != nil) {
    // More information about GTMHTTPFetcher can be found on
    // http://code.google.com/p/gtm-http-fetcher
    GTMHTTPFetcher *fetcher =
      [service.fetcherService fetcherWithURLString:file.downloadUrl];

    [fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
      if (error == nil) {
        // Success.
        completionBlock(data, nil);
      } else {
        NSLog(@"An error occurred: %@", error);
        completionBlock(nil, error);
      }
    }];
  } else {
    completionBlock(nil,
                    [NSError errorWithDomain:NSURLErrorDomain
                                        code:NSURLErrorBadUrl
                                    userInfo:nil]);
  }
}

Or, if you can pass additional parameter to AVPlayer so that it sends additional header to authorize while downloading file, add the following header:

Authorization: Bearer {YOUR_ACCESS_TOKEN}