2

我对 Youtube 视频有一些疑问:

1- 我想将一些 Youtube 视频添加到表格视图中。

2-我想在这个表格视图中添加一个字幕,显示每个视频的评分(喜欢和不喜欢),就像旧 Youtube iPhone 内置应用程序中的附加图片一样

旧的 Youtube 应用

我想做的唯一区别是以数字显示评分,例如:(喜欢:29,不喜欢:3),而不是像以前的 Youtube iPhone 内置应用程序那样以百分比显示。

任何帮助将不胜感激。

4

1 回答 1

3

您应该使用 Youtube API。例如,如果您正在使用搜索栏并根据相关文本搜索相关视频,那么您应该使用以下链接,并解析获得的结果,我使用 NSXML Parser delegates 进行解析。希望这会帮助你。

- (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;
    }
}
于 2012-11-20T18:50:55.760 回答