我无法获取YouTube视频的视频网址。我可以检索带有缩略图和标题的播放列表项,但无法获取实际的视频URL。
GTLServiceYouTube *service = [[GTLServiceYouTube alloc] init];
service.APIKey = @"API Key";
GTLQueryYouTube *query = [GTLQueryYouTube queryForPlaylistItemsListWithPart:@"snippet,contentDetails"];
query.playlistId = @"playlist ID";
query.maxResults = 50;
GTLServiceTicket *ticket = [service executeQuery:query
completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
// This callback block is run when the fetch completes
if (error == nil)
{
GTLYouTubeSearchListResponse *products = object;
for (GTLYouTubeSearchResult *item in products)
{
NSLog(@"%@", item.snippet);
NSString *dictionary = [item.snippet JSONValueForKey:@"videoId"];
GTLYouTubeThumbnailDetails *thumbnails = item.snippet.thumbnails;
GTLYouTubeThumbnail *thumbnail = thumbnails.high;
NSString *thumbnailString = thumbnail.url;
if (thumbnailString != nil)
{
[self.thumbnailsArray addObject:thumbnailString];
[self.thumbnailTitleArray addObject:item.snippet.title];
//[self.videos addObject:video];
NSLog(@"id: %@", dictionary);
}
}
}
else
{
NSLog(@"Error: %@", error.description);
}
[self.tableView reloadData];
}];
有谁知道如何使用YouTube API获取视频网址?
首先,如果您只希望搜索结果中包含视频,则应将type设置为video。在您的代码中:
query.playlistId = @"playlist ID";
query.maxResults = 50;
query.type = @"video";
您需要在部分添加“ id”。您的查询电话将是:
GTLQueryYouTube *query = [GTLQueryYouTube queryForPlaylistItemsListWithPart:@"id,snippet,contentDetails"];
作为响应,您可以使用id.videoId获得视频ID 。因此,在您的代码中它将是:
NSString *videoId = item.identifier.videoId;
获得ID后,您可以将其插入:
http://www.youtube.com/watch?v={VIDEO ID HERE}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句