按ID加载单个视频的YouTube GData Feed

时间:2010-11-06 08:13:05

标签: iphone objective-c youtube youtube-api

我正在尝试在UIWebView中播放youtube视频,而不是离开我的应用程序。

Google认为这很容易 - http://apiblog.youtube.com/2009/02/youtube-apis-iphone-cool-mobile-apps.html

所以我让GData框架和标头工作得很好,我在查询,加载用户的视频输入等方面没有问题。

但我似乎无法加载特定视频的Feed。 我知道我希望提前输入的视频的ID 。如何加载特定视频的Feed?

然后我将按照谷歌的指示:

Grab the video url from the media tag in the API response with the application/x-shockwave-flash type.  

然后像这样嵌入它:

// webView is a UIWebView, either initialized programmatically or loaded as part of a xib.

NSString *htmlString = @"<html><head>
<meta name = \"viewport\" content = \"initial-scale = 1.0, user-scalable = no, width = 212\"/></head>
<body style=\"background:#F00;margin-top:0px;margin-left:0px\">
<div><object width=\"212\" height=\"172\">
<param name=\"movie\" value=\"http://www.youtube.com/v/oHg5SJYRHA0&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\"></param>
<param name=\"wmode\" value=\"transparent\"></param>
<embed src=\"http://www.youtube.com/v/oHg5SJYRHA0&f=gdata_videos&c=ytapi-my-clientID&d=nGF83uyVrg8eD4rfEkk22mDOl3qUImVMV6ramM\"
type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"212\" height=\"172\"></embed>
</object></div></body></html>";

[webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:@"http://www.your-url.com"]];

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:6)

如果有YouTube视频条目,您可以通过以下方式获取每个条目的ID和Flash网址:

for (GDataEntryYouTubeVideo *videoEntry in [feed entries]) {
  GDataYouTubeMediaGroup *mediaGroup = [videoEntry mediaGroup];
  NSString *videoID = [mediaGroup videoID];

  NSArray *mediaContents = [mediaGroup mediaContents];
  GDataMediaContent *flashContent =
    [GDataUtilities firstObjectFromArray:mediaContents
                               withValue:@"application/x-shockwave-flash"
                              forKeyPath:@"type"];

  NSLog(@"video ID = %@, flash content URL = %@",
        videoID, [flashContent URLString]);   
}

答案 1 :(得分:2)

在完全相同的情况下才找到答案。通常是许多大型API,常见的使用说明在解释更高级别的东西时会丢失 - 可能令人沮丧。谢天谢地,信息就在那里......

http://code.google.com/apis/youtube/2.0/developers_guide_protocol_video_entries.html

因此,我们应该能够将返回的ATOM提要插入GData库,并让它解析出适当的内容URL,以便在&#; UIWebView&#39;中使用。风格的球员代码。

... Grobbins,下次正确阅读这个问题!