Load YouTube GData feed for a single video by id

2019-02-15 03:33发布

问题:

I am trying to play a youtube video in a UIWebView instead of leaving my application.

Google thinks is easy peasy- http://apiblog.youtube.com/2009/02/youtube-apis-iphone-cool-mobile-apps.html

So I have the GData framework and headers working nicely, and I have no problem doing queries, loading user's video feeds etc.

But what I can't seem to do is load a specific video's feed. I know the ids of the videos that I want the feeds for in advance. How do i load a specific video's feed?

I'm then going to follow google's instruction :

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

and then embed it like so:

// 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"]];

Any help would be much appreciated!

回答1:

Given a feed of YouTube video entries, you can get the IDs and Flash URLs from each entry this way:

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


回答2:

In exactly the same situation just found the answer. Typically of many large APIs, common usage instructions get lost in explanation of the higher level stuff - can be frustrating. Thankfully, the info is there...

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

So, we should be able to plug the returned ATOM feed into the GData library and have it parse out the proper content URL for use in the 'UIWebView' style player code.

...Grobbins, read the question properly next time!