【问题标题】:How to get a GDATA for a Single Youtube video?如何获取单个 Youtube 视频的 GDATA?
【发布时间】:2013-10-02 20:18:40
【问题描述】:

如何使用来自 youtube 的 gdata 获取单个视频信息。 我知道我可以使用以下代码获取播放列表: 但是该代码无法获取单个视频的数据

NSURL *blogURL = [NSURL URLWithString:@"https://gdata.youtube.com/feeds/api/playlists/PL85E8F3CC22BCEDC6?v=2&alt=jsonc"];

NSData *jsonData = [NSData dataWithContentsOfURL:blogURL];

NSError *error = nil;

NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];


NSDictionary *data1 = [dataDictionary objectForKey:@"data"];

NSDictionary *item = [data1 objectForKey:@"items"];

for (NSDictionary *video in item) {
    NSDictionary *title = [video objectForKey:@"video"];
    NSArray *titleArray = [title objectForKey:@"title"];

    NSLog(@"%@",titleArray);

    NSString*t  = [title objectForKey:@"title"];
    NSLog(@"Title:%@", t);

    ...
}

但我不知道如何获取单个视频。 任何帮助将不胜感激... 谢谢...

【问题讨论】:

标签: ios objective-c youtube-api


【解决方案1】:

要使用 v2 获取单个视频的供稿,您需要将第一行中的 URL 替换为如下内容:

https://gdata.youtube.com/feeds/api/videos/tge2BfiIXiE?v=2&alt=jsonc

根据需要更改 videoID。

与此提要一起返回的 JSON 自然略有不同;因此,与其为项目列表创建一个 NSDictionary 对象然后循环遍历它,不如执行以下操作:

NSURL *feedURL = [NSURL URLWithString:@"https://gdata.youtube.com/feeds/api/videos/tge2BfiIXiE?v=2&alt=jsonc"];

NSData *jsonData = [NSData dataWithContentsOfURL:feedURL];

NSError *error = nil;

NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];

NSDictionary *data1 = [dataDictionary objectForKey:@"data"];    
NSString *t  = [data1 objectForKey:@"title"];
NSLog(@"Title:%@", t);

NSDictionary *thumbs = [data1 objectForKey:@"thumbnail"]; 

NSURL *standardThumb  = [thumbs objectForKey:@"sqDefault"];
NSURL *hdThumb  = [thumbs objectForKey:@"hqDefault"];

编辑代码以反映评论中的问题

【讨论】:

  • 谢谢...您的代码运行良好。你知道如何从中获取缩略图
  • 幸运的是,缩略图路径也在提要中;我已经更新了上面的代码来演示如何访问它。因为我不是 iOS 程序员,所以我不知道你是如何实际检索图像的,但是一旦你有了添加代码的路径,这应该是微不足道的。
  • 我已经尝试使用此代码,但我似乎无法让它工作,因为它没有填充我的 UIImageView:NSURL *standardThumb = [thumbs objectForKey:@"sqDefault"]; NSURL *hdThumb = [thumbs objectForKey:@"hqDefault"]; UIImage *thumbnail =[UIImage imageWithData:[NSData dataWithContentsOfURL:hdThumb]]; [currentItem1 setImage:thumbnail];
  • 如果你记录它 -- NSLog(@"HD Thumbnail:%@", hdThumb); -- 它是否显示有效的 URL?
  • 我登录了它,它正在停用 null value。这就是我的输出中出现的内容:2013-10-05 10:15:51.839 MiTube[304:19d03] HD Thumbnail:(null)
猜你喜欢
  • 1970-01-01
  • 2011-10-16
  • 2014-06-02
  • 1970-01-01
  • 2012-05-06
  • 2012-03-08
  • 2011-05-05
  • 1970-01-01
  • 2014-05-31
相关资源
最近更新 更多