如何使用iOS-GTLYouTube库播放YouTube视频?

时间:2014-11-12 09:57:19

标签: ios objective-c youtube youtube-api playback

我尝试使用iOS-GTLYouTube库播放YouTube视频。我刚刚完成了我的视频的查询代码,并找回了一些GTLYouTubeVideo对象。所以,在我得到它们之后,又是什么?我该怎么玩?我在网上搜索过但没有找到任何内容。

1 个答案:

答案 0 :(得分:0)

您无法使用GTLYouTube库播放视频,您只能使用它来搜索API以获取视频信息。

在检索要播放的视频的视频ID后,您可以在UIWebView中将该ID加载到iFrame中。

NSString *videoID = @"HCdUubr70i8"; // the video ID returned from your API query

// Adjust the width and height to your liking, I just pass in the view's frame width and height
NSString *str = [NSString stringWithFormat:@"<html><body style='margin:0px;padding:0px;'><script type='text/javascript' src='http://www.youtube.com/iframe_api'></script><script type='text/javascript'>function onYouTubeIframeAPIReady(){ytplayer=new YT.Player('playerId',{events:{onReady:onPlayerReady}})}function onPlayerReady(a){a.target.playVideo();}</script><iframe id='playerId' type='text/html' width='%@' height='%@' src='http://www.youtube.com/embed/%@?enablejsapi=1&rel=0&playsinline=1&autoplay=1' frameborder='0'></body></html>", self.view.frame.size.width, self.view.frame.size.height, videoID];


UIWebView *webView = [UIWebView ...];
[webView loadHTMLString:embedHTML baseURL: [[NSBundle mainBundle] bundleURL]];

Google还提供了自己的UIWebView包装类youtube-ios-player-helper,这样可以让iOS设备上的YouTube视频播放更容易。