UIWebview does not display embedded youtube well for iOS 8 -


I have difficulty running YouTube in UIWebview.

If I use the default embed format from YouTube, it does not show anything in the UIWebview just empty

  NSString * htmlString = @ " Gt; & lt; body & gt; & lt; iframe width = \ "420 \" height = \ "315 \" src = \ "// www.youtube .com / embed / XNnaxGFO18o? Rel = 0 \ "frameborder = \" 0 \ "AcceptableSluscreen> gt; & lt; / iframe & gt; & lt; / body & gt; & lt; / html & gt;"; [Self.webView loads HTML string: htmlString baseURL: zero];   

I gotogled around and found the following code works.

  NSString * embedHTML = @ "\ & lt; html> gt; body style = \" margin: 0; \ "& Gt; \ & lt; div class = \" emvideo emvideo-video emvideo-youtube \ "& gt; embed id = \" yt \ "src = \" http://www.youtube. Com / embed / bPM8LKYMcXs? Hd = 1 \ "width = \" 320 \ "height = \" 250 \ "& gt; & lt; / Embed & gt; & lt; / div & gt; \ & lt; / body & gt; & lt; / Html & gt; "; [Self.webView loads HTML string: embedHTML baseURL: zero]; However, there is a problem if I click on the play button, then there is no answer. I have to click the play button several times and wait for a while, then this Spin starts and then plays.  

So I have two questions.

1) Do you have a better way of embedding YouTube videos in UIWebView? 2) How to play video immediately after clicking play button?

Thank you.

Create a plain html file and call it youtubeEmbedding.html < Name it as / code> and add it to youtubeEmbedding.html in your project, just add the code below to . Html file

  & lt ;! DOCTYPE html & gt; & Lt; Html & gt; & Lt; Top & gt; & Lt; Style & gt; Entity {margin: 0 px 0 px 0 px 0 px;} & lt; / Style & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; Div id = "player" & gt; & Lt; / Div & gt; & Lt; Script & gt; 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',% @ Player Wars: {'controls': % D, 'placeline': 0, 'relay': 0, 'shinfu': 0},}); }player read (event) on function {event.target.playVideo (); } Stop the video () {if (ytplayer) {ytplayer.stopVideo (); ytplayer.clearVideo (); } Return 'STOPPED'; } Function Play Video () {if (ytplayer) {ytplayer.playVideo (); } Return 'PLAY'; } Festival Pause Video () {if (ytplayer) {ytplayer.pause video; ytplayer.clearVideo (); } Return 'PAUSED'; } GetPlayerState (function) {return ytplayer.getPlayerState (); } FunctionPlaying () {return (myPlayerState == YT.PlayerState.PLAYING); } Function is pot () {returns (myPlayerState == YT.PlayerState.PAUSED); } PlayerError (event) on the function {warning ("error"); } & Lt; / Script & gt; & Lt; / Body & gt;   

And in the controller where you are using the web view just like the following

  - (zero) ViewDidload {BOOL Autoplay = No; // Setup an additional after loading the view, usually from any nib NSString * youTubeVideoHTML = [[NSString alloc] initWithContentsOfFile: [[NSBundle main bundle] Pathfoser resource: @ "youtubeEmbedding" type: @ "html"] Encoding: NSUTF8StringEncoding Error: Zero]; NSString * Autoplast = Autoplay? @ "Event: {'On-Ed': Onplayer Readie},": @ ""; // Customize the weather. Whether you want to do otomato or not NSURL * url = [NSURL URLWithString: @ "https://www.youtube.com/watch?v=AYo72E7ZMHM"]; NSString * lastComponent = [url query]; NSString * videoId = [Last Companion String By Replacing OccurrenceOffString: @ "V =" With String: @ ""]; Get video ID from // url NSInteger controls = 1; // I want to show changes for play, breaks, fullscreen etc. NSString * html = [NSString stringWithFormat: YouTubeVideo HTML, Video ID, CGRactGet Widethth (_WebView.frame), CGRactGhatHight (_webView.frame), AutoplastString, Control]; // YouTube video width and height set to see the entire webview _webView.mediaPlaybackRequiresUserAction = NO; _webView.delegate = self; // Not necessary [_webView Load HTML String: html baseURL: [[NSBundle main bundle] sourceURL]]; // WebView to load it) // Play action - (IBAction) Play Action: (ID) Sender {[_webView stringByEvaluatingJavaScriptFromString: @ "ytplayer.playVideo ()"]; }   

If u have any problems, hope that this helps you .. :)

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -