如何将Youtubevideo添加到UITableView?
我对Youtubevideo有一些疑问:
1-我想将一些Youtubevideo添加到表格视图中。
2-我想在此表视图中添加一个字幕,显示每个video的评级编号(喜欢和不喜欢),例如来自旧Youtube iPhone内置应用程序的附加图片
我想要做的唯一区别是以数字显示评分,例如:(喜欢:29,不喜欢:3),而不是像旧的Youtube iPhone内置应用程序那样的百分比。
任何帮助将不胜感激。
你应该使用Youtube API。 例如,如果您使用搜索栏并搜索相关文本相关的video,那么您应该使用以下链接,并解析获取结果,我已使用NSXML Parser委托进行解析。 希望这会帮助你。
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar { [_searchBar resignFirstResponder]; self.typeString = [NSString stringWithFormat:@"%@",searchBar.text]; _xmlUrl = [NSString stringWithFormat:@"http://gdata.youtube.com/feeds/api/videos?vq=%@&orderby=relevance&start-index=1&max-results=10&alt=atom",[self.typeString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:_xmlUrl]]; _urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; [request release]; [_searchTableView reloadData]; }
编译指示标记 – NSXMLParser委托方法
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ if ([elementName isEqualToString:@"entry"]) { isEntry = YES; isThumbnail=NO; isUrl=NO; _xmlObject = [[YoutubeData alloc] init]; } else if ([elementName isEqualToString:@"title"] && isEntry){ isTitle = YES; } else if ([elementName isEqualToString:@"media:category"] && isEntry){ isMediaCategory=YES; } else if ([elementName isEqualToString:@"yt:statistics"] && isEntry){ int totalViewers = [[attributeDict objectForKey:@"viewCount"] intValue]; _xmlObject.songViewers = [NSString stringWithFormat:@"%d",totalViewers]; int favorite = [[attributeDict objectForKey:@"favoriteCount"] intValue]; float percentage = (favorite*100.0)/totalViewers; _xmlObject.songLikePercent = [NSString stringWithFormat:@"%.2f %@",percentage, @"%"]; } else if ([elementName isEqualToString:@"yt:duration"] && isEntry){ _xmlObject.songDuraion = [attributeDict objectForKey:@"seconds"]; NSLog(@"Duration is: %@",_xmlObject.songDuraion); } else if ([elementName isEqualToString:@"media:player"] && (isUrl==NO) && isEntry) { NSString *strLink = [NSString stringWithFormat:@"%@",[attributeDict objectForKey:@"url"]]; strLink = [strLink stringByReplacingOccurrencesOfString:@"&feature=youtube_gdata_player" withString:@""]; _xmlObject.songUrl = strLink; NSLog(@"Url is: %@",_xmlObject.songUrl); isUrl=YES; } else if ([elementName isEqualToString:@"media:thumbnail"] && (isThumbnail==NO) && isEntry){ _xmlObject.songImage = [attributeDict objectForKey:@"url"]; NSLog(@"My Thumbnail Url is :%@",_xmlObject.songImage); NSLog(@"Images is: %@",_xmlObject.songImage); isThumbnail=YES; } } - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ if (isTitle) { NSLog(@"Title is: %@", string); _xmlObject.songTitle = string; } else if (isMediaCategory) { NSLog(@"Song Id is: %@", string); _xmlObject.songType = string; } } - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ if ([elementName isEqualToString:@"title"] && isEntry) { isTitle = NO; } else if ([elementName isEqualToString:@"media:category"] && isEntry){ isMediaCategory=NO; } else if ([elementName isEqualToString:@"entry"]){ isEntry=NO; [self.youtubeObjects addObject:_xmlObject]; [_xmlObject release]; _xmlObject = nil; } }