UIWebView YouTubeembeddedvideo不加载

我在通过UIWebViewembeddediOS应用程序中获取video时遇到了一些麻烦。 iframe播放器加载完全正常,但是当我点击播放button时,白色的微调器出现一秒钟后就消失了,只剩下一个黑盒子如果我碰到黑盒子,我会得到标题和“i”button,但是video永远不会启动。

我发现获得video播放的唯一方法是点击右上angular的“我”button查看信息,然后再次按下,这将触发全屏播放器。 我只是不明白为什么它不会在第一印刷机上播放。

这里是我用来创buildHTML和embeddedYouTubevideo的完整代码

- (void)setupHTMLString { //NSData *data = [[NSData alloc] init]; NSString *imageString = [NSData htmlForJPEGImage:[UIImage imageWithData:self.post.thumbnail]]; NSString *htmlString = @"<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\"></head><body>"; htmlString = [htmlString stringByAppendingString:@"<div id=\"content\" style=\"width:304px\">"]; NSString *titleString = [NSString stringWithFormat:@"<h1>%@</h1> ", self.post.title]; NSString *authorString = [NSString stringWithFormat:@"<h3>By %@</h3>", [self returnAuthorName]]; NSString *contentString = [NSString stringWithFormat:@"<p>%@</p>", [self createStringWithURLsFromString:self.post.content]]; NSString *postString = [NSString stringWithFormat:@"%@ %@ %@ %@", imageString, titleString, authorString, contentString]; htmlString = [htmlString stringByAppendingString:postString]; htmlString = [htmlString stringByAppendingString:@"</div?></body></html>"]; NSLog(@"%@", htmlString); // UIWebView uses baseURL to find style sheets, images, etc that you include in your HTML. NSURL *bundleUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]; [self.webView loadHTMLString:htmlString baseURL:bundleUrl]; self.webView.scrollView.contentInset = UIEdgeInsetsMake(0, 0, INSETHEIGHT, 0); self.webView.scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, INSETHEIGHT, 0); } - (NSString *)createStringWithURLsFromString:(NSString *)string { NSString *regexToReplaceRawLinks = @"(?:https?:)?//(?:[^.]*\\.)?youtu(?:\\.be|be\\.com)(?:/|/embed/|/v/|/watch/?\?(?:.+&)?v=)([\\w-]{11})"; NSError *error = NULL; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexToReplaceRawLinks options:NSRegularExpressionCaseInsensitive error:&error]; NSString *modifiedString = [regex stringByReplacingMatchesInString:string options:0 range:NSMakeRange(0, [string length]) withTemplate:@"<iframe class=\"youtube-player\" type=\"text/html\" width=\"320\" height=\"180\" src=\"http://www.youtube.com/embed/$1\" frameborder=\"0\"></iframe>"]; //NSLog(@"%@", modifiedString); return modifiedString; } 

你不必做任何事情。 这是Google最近推出的一个漏洞。 谷歌修复了这个错误,并且刚刚部署了它。 如果你重新尝试你的代码,它应该工作。

如果您有兴趣,请点击以下链接: https : //code.google.com/p/gdata-issues/issues/detail?id=6061

 - (void)viewDidLoad { [super viewDidLoad]; NSString *embedCode = @"<iframe width=\"265\" height=\"140\" src=\"http://www.youtube.com/embed/NUMBERSANDLETTERS\" frameborder=\"0\" allowfullscreen></iframe>"; [[self webview] loadHTMLString:embedCode baseURL:nil]; } 

在“urlStr”中传递YouTubevideo的url。

 //In .h file UIWebView *videoView; // In .m file videoView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 385)]; [self embedYouTube :urlStr frame:CGRectMake(0, 0, 320, 385)]; [self.view addSubview:videoView]; // methos to embed URL in HTML & load it to UIWebView - (void)embedYouTube:(NSString*)url 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, url, frame.size.width, frame.size.height]; if(videoView == nil) { videoView = [[UIWebView alloc] initWithFrame:frame]; [self.view addSubview:videoView]; } [videoView loadHTMLString:html baseURL:nil]; } 

注意:当您在模拟器上运行UIWebView for YouTubevideo时,无法加载,完美地在设备上工作。