对于iOS 8,UIWebview不会显示embedded式的YouTube

我很难在UIWebview中embeddedyoutube。

如果我使用的是来自YouTube的默认embedded格式,则不会在UIWebview中显示任何内容。 只是空白。

NSString *htmlString = @"<html><body><iframe width=\"420\" height=\"315\" src=\"//www.youtube.com/embed/XNnaxGFO18o?rel=0\" frameborder=\"0\" allowfullscreen></iframe></body></html>"; [self.webView loadHTMLString:htmlString baseURL:nil]; 

我search了一下,发现下面的代码工程。

 NSString* embedHTML = @"\ <html><body style=\"margin:0;\">\ <<div class=\"emvideo emvideo-video emvideo-youtube\"><embed id=\"yt\" src=\"http://www.youtube.com/embed/bPM8LKYMcXs?hd=1\" width=\"320\" height=\"250\"></embed></div>\ </body></html>"; [self.webView loadHTMLString:embedHTML baseURL:nil]; 

但是,有一个问题。 如果我点击播放button,它没有任何反应。 我必须点击播放button几次,然后等待一段时间,然后开始旋转然后播放。

所以我有两个问题。

1)你有没有更好的方法来播放UIWebViewembeddedYouTubevideo? 2)单击播放button后,如何立即播放video?

谢谢。

创build一个纯html文件,并将其命名为youtubeEmbedding.html并将其添加到youtubeEmbedding.html中的项目中,只需将以下代码添加到.html文件

 <!DOCTYPE html> <html> <head> <style>body{margin:0px 0px 0px 0px;}</style> </head> <body> <div id="player"></div> <script> var tag = document.createElement('script'); tag.src = 'http://www.youtube.com/player_api'; var firstScriptTag = document.getElementsByTagName('script')[0]; firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); firstScriptTag.requestFullScreen(); var ytplayer = null; function onYouTubePlayerAPIReady() { ytplayer = new YT.Player( 'player', { videoId:'%@', width:'%0.0f', height:'%0.0f', %@ playerVars: { 'controls': %d, 'playsinline' : 0, 'rel':0, 'showinfo':0 }, }); } function onPlayerReady(event) { event.target.playVideo(); } function stopVideo() { if (ytplayer) { ytplayer.stopVideo(); ytplayer.clearVideo(); } return 'STOPPED'; } function playVideo() { if (ytplayer) { ytplayer.playVideo(); } return 'PLAY'; } function pauseVideo() { if (ytplayer) { ytplayer.pauseVideo(); ytplayer.clearVideo(); } return 'PAUSED'; } function getPlayerState() { return ytplayer.getPlayerState(); } function isPlaying() { return (myPlayerState == YT.PlayerState.PLAYING); } function isPaused() { return (myPlayerState == YT.PlayerState.PAUSED); } function onPlayerError(event) { alert("Error"); } </script> </body> 

并在控制器中,你正在使用networking视图就像下面这样做

 - (void)viewDidLoad { BOOL autoPlay = NO; // Do any additional setup after loading the view, typically from a nib. NSString *youTubeVideoHTML = [[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"youtubeEmbedding" ofType:@"html"] encoding:NSUTF8StringEncoding error:nil]; NSString *autoPlayString = autoPlay ? @"events: {'onReady' : onPlayerReady }," : @""; //customise it weather u want to autoplay or not NSURL *url = [NSURL URLWithString:@"https://www.youtube.com/watch?v=AYo72E7ZMHM"]; NSString *lastComponent = [url query]; NSString *videoId = [lastComponent stringByReplacingOccurrencesOfString:@"v=" withString:@""]; //get the video id from url NSInteger controls = 1; //i want to show controles for play,pause,fullscreen ... etc NSString *html = [NSString stringWithFormat:youTubeVideoHTML, videoId, CGRectGetWidth(_webView.frame), CGRectGetHeight(_webView.frame), autoPlayString, controls]; //setting the youtube video width and height to fill entire the web view _webView.mediaPlaybackRequiresUserAction = NO; _webView.delegate = self; //not needed [_webView loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]]; //load it to webview } //for playing action - (IBAction)playAction:(id)sender { [_webView stringByEvaluatingJavaScriptFromString:@"ytplayer.playVideo()"]; } 

如果你有任何问题,评论,希望这可以帮助你.. 🙂