UIWebview does not display embedded youtube well for iOS 8 -
UIWebview does not display embedded youtube well for iOS 8 -
i have difficulty play embed youtube in uiwebview.
if used default embed format youtube, not show in uiwebview. blank.
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];
i googled around , found next code works.
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];
however, there problem. if click play button, has no response. have click play button couple of times , wait while, starts spin , play.
so have 2 questions.
1) have improve way play embed youtube video in uiwebview? 2) how play video instantly after click play button?
thanks.
create plain html
file , name youtubeembedding.html
, add together project in youtubeembedding.html add together below code .html
file
<!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(); } homecoming 'stopped'; } function playvideo() { if (ytplayer) { ytplayer.playvideo(); } homecoming 'play'; } function pausevideo() { if (ytplayer) { ytplayer.pausevideo(); ytplayer.clearvideo(); } homecoming 'paused'; } function getplayerstate() { homecoming ytplayer.getplayerstate(); } function isplaying() { homecoming (myplayerstate == yt.playerstate.playing); } function ispaused() { homecoming (myplayerstate == yt.playerstate.paused); } function onplayererror(event) { alert("error"); } </script> </body>
and in controller u using web view below
- (void)viewdidload { bool autoplay = no; // additional setup after loading view, typically nib. nsstring *youtubevideohtml = [[nsstring alloc] initwithcontentsoffile:[[nsbundle mainbundle] pathforresource:@"youtubeembedding" oftype:@"html"] encoding:nsutf8stringencoding error:nil]; nsstring *autoplaystring = autoplay ? @"events: {'onready' : onplayerready }," : @""; //customise weather u want 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 video id url nsinteger controls = 1; //i want show controles play,pause,fullscreen ... etc nsstring *html = [nsstring stringwithformat:youtubevideohtml, videoid, cgrectgetwidth(_webview.frame), cgrectgetheight(_webview.frame), autoplaystring, controls]; //setting youtube video width , height fill entire web view _webview.mediaplaybackrequiresuseraction = no; _webview.delegate = self; //not needed [_webview loadhtmlstring:html baseurl:[[nsbundle mainbundle] resourceurl]]; //load webview } //for playing action - (ibaction)playaction:(id)sender { [_webview stringbyevaluatingjavascriptfromstring:@"ytplayer.playvideo()"]; }
comment if u hav problem, hope helps u .. :)
ios uiwebview youtube ios8
Comments
Post a Comment