播放列表中的Youtube GData视频

时间:2012-09-24 06:56:57

标签: objective-c ios5 youtube-api gdata-api

美好的一天,我是Youtube GData api的新手,我无法访问特定播放列表的视频列表。

我首先做的是获取特定用户的播放列表列表。它返回了类似GDataEntryYouTubePlaylistLink 0x888ea20: {v:2.1 title:Playlist Title summary: contentSrc:https://gdata.youtube.com/feeds/api/playlists/PL5......92D127 etag:W/"D04N.....WhJVEU8." authors:1 categories:1 links:related,alternate,self id:tag:youtube.com,2008:user:MyYoutubeUser:playlist:PL5......92D127 countHint:7 unparsed:<media:group>}

的内容

我了解到countHint是该播放列表中的视频数量。现在,我想收看这些视频。

我尝试创建一个可以这样做的函数,但我不知道我将使用什么FeedID:

    GDataServiceGoogleYouTube *service = [self youTubeService];

NSString *feedID = kGDataYouTubeUserFeedIDPlaylists; //this was the feedID i used to get the list of playlist of a user
NSURL *feedURL = [GDataServiceGoogleYouTube youTubeURLForUserID:@"MyYoutubeUser"
                                                        userFeedID:feedID];

[service fetchFeedWithURL:feedURL
                 delegate:self
        didFinishSelector:@selector(request:finishedLoading:error:)];

1 个答案:

答案 0 :(得分:1)

我能够找到解决问题的方法。我希望它也可以帮到你。

在我的viewDidLoad中,我调用了loadYoutube和loadYoutubePlaylist函数。

  • loadYoutube - &gt;为我们提供给定用户的URL和什么feedID。就我而言,feedID是kGDataYouTubeUserFeedIDUploads,因为我只想获取用户上传的播放列表和视频。
  • loadYoutubePlaylist - &gt;为我们提供了用户播放列表的URL。 feedID是kGDataYouTubeUserFeedIDPlaylists
  • loadYoutubeVideosPerPlaylist - &gt;为我们提供每个播放列表视频的网址。

这就是代码的样子:

- (void)loadYoutube
{
    GDataServiceGoogleYouTube *service = [self youTubeService];

    NSString *uploadsID = kGDataYouTubeUserFeedIDUploads;
    NSURL *feedURL = [GDataServiceGoogleYouTube youTubeURLForUserID:@"USERNAME"
                                                         userFeedID:uploadsID];
    NSLog(@"loadYoutube URL: %@", feedURL);
    [service fetchFeedWithURL:feedURL
                     delegate:self
            didFinishSelector:@selector(request:finishedLoadingYoutubeWithFeed:error:)];
}

- (void)loadYoutubePlaylist
{
    GDataServiceGoogleYouTube *service = [self youTubeService];

    NSString *feedID = kGDataYouTubeUserFeedIDPlaylists;
    NSURL *feedURL = [GDataServiceGoogleYouTube youTubeURLForUserID:@"USERNAME"
                                                            userFeedID:feedID];
    NSLog(@"loadYoutubePlaylist URL: %@", feedURL);
    [service fetchFeedWithURL:feedURL
                     delegate:self
            didFinishSelector:@selector(request:finishedLoadingPlaylistWithFeed:error:)];
}

- (void)loadYoutubeVideosPerPlaylist:(NSString *)urlString
{
    GDataServiceGoogleYouTube *service = [self youTubeService];
    NSURL *feedURL = [NSURL URLWithString:urlString];

    NSLog(@"loadYoutubeVideosPerPlaylist URL: %@", feedURL);

    [service fetchFeedWithURL:feedURL
                     delegate:self
            didFinishSelector:@selector(request:finishedLoadingYoutubeVideosPerPlaylistWithFeed:error:)];
}

以下是Youtube的请求..

#pragma mark - Youtube Requests

- (void)request:(GDataServiceTicket *)ticket finishedLoadingYoutubeWithFeed:(GDataFeedBase *)aFeed error:(NSError *)error
{

}

- (void)request:(GDataServiceTicket *)ticket finishedLoadingPlaylistWithFeed:(GDataFeedBase *)aFeed error:(NSError *)error
{
    if (kGDataYouTubeUserFeedIDPlaylists) {
        self.playlistFeed = (GDataFeedYouTubeVideo *)aFeed;

        NSArray *feedArray = [self.playlistFeed entries];
        for (int i = 0; i < [feedArray count]; i++) {
            GDataEntryBase *entry = [[self.playlistFeed entries] objectAtIndex:i];
            NSString *title = [[entry title] stringValue];
            [playlist addObject:NSLocalizedString(title, @"playlistTitle")];
            NSLog(@"%@", [[entry content] sourceURI]);
            [videoPlaylistURLArray addObject:[[entry content] sourceURI]];
        }
    }
    if (!error) {
        [self addMenuButtons];
        [self setPlaylistIndex:0];
        [SVProgressHUD showSuccessWithStatus:@"Success!"];
    }else{
        [SVProgressHUD showErrorWithStatus:@"Failed Fetching Playlist."];
    }
}

- (void)request:(GDataServiceTicket *)ticket finishedLoadingYoutubeVideosPerPlaylistWithFeed:(GDataFeedBase *)aFeed error:(NSError *)error
{
    self.feed = (GDataFeedYouTubeVideo *)aFeed;
    [self.youtubeTable reloadData];
}