1

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?

vburojevic
  • 1,666
  • 5
  • 24
  • 34

2 Answers2

2

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

Nitesh
  • 1,389
  • 1
  • 15
  • 24
1

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}
JunYoung Gwak
  • 2,967
  • 3
  • 21
  • 35
  • Can you please post an example how to load authorized link in AVPlayer? I understand what you mean and it makes sense, I just don't know how to implement it. I would really appreciate the help, I am stuck with this for some time. – vburojevic Aug 09 '13 at 18:38
  • Take a look at this question: http://stackoverflow.com/questions/15456130/send-headers-with-avplayer-request-in-ios it seems like it is not possible to send custom header to AVPlayer. However you can download via generic HTTP and use its cache to stream. You can try this hacky way or find another player that supports custom header and add access token to the header of your http request. – JunYoung Gwak Aug 09 '13 at 20:46
  • Can the AVPlayer stream while downloading? I was thinking about starting a download and then play from received NSData while it is populating. Is it possible? – vburojevic Aug 09 '13 at 20:49
  • @JunYoungGwak I m not able to get public url of the selected google Drive File. How we can get public url of the google drive files and play file without Download. Please u could suggest me to proceed – ChenSmile Sep 11 '13 at 04:09
  • Is it actual in 2020? Can you add solution for Swift, pls? – VyacheslavBakinkskiy Oct 26 '20 at 05:00