Reputation: 1666
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?
Upvotes: 1
Views: 1558
Reputation: 1389
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
Upvotes: 2
Reputation: 2987
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}
Upvotes: 1