我正在尝试使用查询来简单地搜索视频,使用以下代码可以完美地工作。
// Create a service object for executing queries
GTLServiceYouTube *service = [[GTLServiceYouTube alloc] init];
// Services which do not require sign-in may need an API key from the
// API Console
service.APIKey = @"AIzaSyA-e4NldR2o8vYPpL6IcAcMH3HSnEpPPJY";
// Create a query
GTLQueryYouTube *query = [GTLQueryYouTube queryForSearchListWithPart:@"id,snippet"];
query.maxResults = 10;
query.q = searchBar.text;
query.videoEmbeddable = @"true";
query.type = @"video";
//query.country = @"US";
// Execute the query
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;
[videoArray removeAllObjects];
// iteration of items and subscript access to items.
for (GTLYouTubeSearchResult *item in products) {
NSMutableDictionary *dictionary = [item JSONValueForKey:@"id"];
NSLog(@"%@", [dictionary objectForKey:@"videoId"]);
YoutubeVideo *video = [[YoutubeVideo alloc]init];
[video setLblTitle:item.snippet.title];
//Get youtube video image
[video setImgIconURL:[NSURL URLWithString:item.snippet.thumbnails.defaultProperty.url]];
[video setLblVideoURL:[dictionary objectForKey:@"videoId"]];
[video setLblChannelTitle:item.snippet.channelTitle];
[videoArray addObject:video];
}
reloadData = YES;
[tableView reloadData];
//Download images asynchronously
[NSThread detachNewThreadSelector:@selector(downloadImages)
toTarget:self
withObject:nil];
}else{
NSLog(@"Error: %@", error.description);
}
}];
但是,现在我想显示有关视频的某些信息。我可以从中得到一些信息
item.snippet
但我还需要获取视频持续时间和观看次数。如何使用 Youtube API 3.0 获取它们?我也有一个想法尝试为此使用 GData,但它实际上使加载时间增加了三倍
NSString *JSONString = [NSString stringWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://gdata.youtube.com/feeds/api/videos/%@?v=2&alt=json", [video lblVideoURL]]] encoding:NSUTF8StringEncoding error:nil ];
如何获取视频的时长以及视频的观看次数?