How to add Youtube videos to a UITableView? [closed]

Deadly 提交于 2019-12-24 02:42:16

问题


I have some question about Youtube videos :

1- I want to add some Youtube videos to a table view.

2- I want to add in this table view a subtitle showing the rating number for each video ( like & dislike ) like the attached picture from the old Youtube iPhone built-in app

The only difference I want to do is to show the rating in numbers for example: ( Like: 29 , Dislike: 3 ), not in percentage like the old Youtube iPhone built-in app used to do.

Any help will be appreciated.


回答1:


You should use Youtube API's. For example if you are using searchbar and searching videos related on the basis of related text, then you should use the following link, and parse the obtaining results, I have used NSXML Parser delegates for Parsing. Hope this will help you.

- (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];
}

pragma mark - NSXMLParser Delegate Method

- (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;
    }
}


来源:https://stackoverflow.com/questions/13477740/how-to-add-youtube-videos-to-a-uitableview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!