1

有没有办法使用 AVPlayer 从 Google Drive 流式传输音频文件?

我已经尝试使用 file.downloadUrl 和 file.webContentLink 并且它不起作用。

代码:

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];

如果无法流式传输,是否可以在 /tmp 文件夹中开始下载并在下载时播放?

4

2 回答 2

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

于 2014-03-28T11:07:57.740 回答
1

这仅仅是因为您没有从下载请求的标头中提供客户端的访问代码。当您获得 downloadUrl 时,该链接不是公共链接,您应该提供与所有其他 Drive API 请求相同的授权。

例如,用于从 downloadUrl 下载内容的 Object-c 代码如下所示:

+ (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]);
  }
}

或者,如果您可以将附加参数传递给 AVPlayer,以便在下载文件时发送附加标头进行授权,请添加以下标头:

Authorization: Bearer {YOUR_ACCESS_TOKEN}
于 2013-08-09T18:34:23.027 回答