embeddedYouTubevideo
我已经通过在互联网上find的代码片段embedded了来自YouTube的video,以下是我使用的代码:
@interface FirstViewController (Private) - (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame; @end @implementation FirstViewController - (void)viewDidLoad { [super viewDidLoad]; [self embedYouTube:@"http://www.youtube.com/watch?v=l3Iwh5hqbyE" frame:CGRectMake(20, 20, 100, 100)]; } - (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame { NSString *embedHTML = @"\ <html><head>\ <style type=\"text/css\">\ body {\ background-color: transparent;\ color: white;\ }\ </style>\ </head><body style=\"margin:0\">\ <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \ width=\"%0.0f\" height=\"%0.0f\"></embed>\ </body></html>"; NSString *html = [NSString stringWithFormat:embedHTML, urlString, frame.size.width, frame.size.height]; UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame]; [videoView loadHTMLString:html baseURL:nil]; [self.view addSubview:videoView]; [videoView release]; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } - (void)viewDidUnload { // Release any retained subviews of the main view. // eg self.myOutlet = nil; } - (void)dealloc { [super dealloc]; } @end
它编译正确,当我打开它,我看到一个白盒定位不正确,代码创build。 我有两个问题:
-
我怎么知道video是否会播放,这只是一个普通的白色框,模拟器播放来自YouTube的video?
-
我怎样才能摆放这个盒子?
刚刚testing过你的代码,它在iPhone上工作正常,Youtubevideo在iOS模拟器上不被支持,所以你需要一个真正的设备进行testing。
我怎样才能摆放这个盒子?
您已经在该行的X(20),Y(20),宽度(100)和高度(100)
[self embedYouTube:@"http://..." frame:CGRectMake(20, 20, 100, 100)];
之后要更改视图的位置,请修改其中心属性:
videoView.center = CGPointMake(200, 100 );
下面是示例代码,它在iOS 5及其后续版本上工作正常,这是Youtube API提供的新实现, myWeb
是一个UIWebView。 URL中的showinfo=0
将删除videoView中的顶部标题。
NSString *html = [NSString stringWithFormat:@"<html><body><iframe class=\"youtube-player\" type=\"text/html\" width=\"%f\" height=\"%f\" src=\"http://www.youtube.com/embed/q1091sWVCMI?HD=1;rel=0;showinfo=0\" allowfullscreen frameborder=\"0\" rel=nofollow></iframe></body></html>",frame.size.width,frame.size.height]; [myWeb loadHTMLString:html baseURL:nil];
希望这可以帮助你:)
这里有一个适用于iOS 6的版本和新的YouTubeembedded代码:
- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame { NSString *html = [NSString stringWithFormat:@"<html><head><style type='text/css'>body {background-color: transparent;color: white;}</style></head><body style='margin:0'><iframe width='%f' height='%f' src='%@' frameborder='0' allowfullscreen></iframe></body></html>", frame.size.width, frame.size.height, urlString]; UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame]; [videoView loadHTMLString:html baseURL:nil]; [self.view addSubview:videoView]; }
对于那些使用Monotouch和C#的人,您可以将此类添加到您的项目中,并使用它来查看您的video:
public class YouTubeViewer : UIWebView { public YouTubeViewer(string url, RectangleF frame) { string youTubeVideoHTML = @"<object width=""{1}"" height=""{2}""><param name=""movie"" value=""{0}""></param><embed src=""{0}"" type=""application/x-shockwave-flash"" width=""{1}"" height=""{2}""</embed></object>"; string html = string.Format(youTubeVideoHTML, url, frame.Size.Width, frame.Size.Height); this.LoadHtmlString(html, null); this.Frame = frame; } }
确保您传递的URL是您点击Sharebutton时从Youtube提供的embedded代码的URL。 (并检查旧代码的checkbox)
快速实施:
let webView = UIWebView(frame: self.view.frame) // Position your player frame here self.view.addSubview(webView) self.view.bringSubviewToFront(webView) // Playback options: play in-line and start playing immediately webView.allowsInlineMediaPlayback = true webView.mediaPlaybackRequiresUserAction = false // Set the id of the video you want to play let videoID = "zN-GGeNPQEg" // https://www.youtube.com/watch?v=zN-GGeNPQEg // Set up your player let embededHTML = "<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='\(self.view.frame.size.width)' height='\(self.view.frame.size.height)' src='http://www.youtube.com/embed/\(videoID)?enablejsapi=1&rel=0&playsinline=1&autoplay=1' frameborder='0'></body></html>" // Load the HTML into your UIWebView webView.loadHTMLString(embededHTML, baseURL: NSBundle.mainBundle().bundleURL)
另外,YouTube为开发者提供了一个很好的助手类 ,可以在iOS设置YouTube播放,为您设置UIWebView
。