How can you embed youtube videos that don't start the external player on Iphone? -
i'm trying implement embedded youtube video in application. way i've managed frameworks lbyoutubeview or hcyotubeparser. i've read against youtube tos because strip video link http getting this.
this indeed can played inside mpmovieplayercontroller without problems (and without leaving application).
from i've search there 2 applications managed vodio , frequency i'm wondering if there special sdk developers or affiliate program might have missed.
from read youtube engineers respond questions if tagged youtube-api hope clear up.
i've read uiwebview implementations scenario opens external video player have no control over.
i've looked around while , came with: involves adding youtube.html file project following code:
<html> <head><style>body{margin:0px 0px 0px 0px;}</style></head> <body> <!-- 1. <iframe> (and video player) replace <div> tag. --> <div id="player"></div> <script> // 2. code loads iframe player api code asynchronously. var tag = document.createelement('script'); tag.src = "http://www.youtube.com/player_api"; var firstscripttag = document.getelementsbytagname('script')[0]; firstscripttag.parentnode.insertbefore(tag, firstscripttag); // 3. function creates <iframe> (and youtube player) // after api code downloads. var player; function onyoutubeplayerapiready() { player = new yt.player('player', { width: '640', height: '360', videoid: '%@', playervars: {'autoplay' : 1, 'controls' : 0 , 'vq' : 'hd720', 'playsinline' : 1, 'showinfo' : 0, 'rel' : 0, 'enablejsapi' : 1, 'modestbranding' : 1}, events: { 'onready': onplayerready, 'onstatechange': onplayerstatechange } }); } // 4. api call function when video player ready. function onplayerready(event) { event.target.playvideo(); } // 5. api calls function when player's state changes. // function indicates when playing video (state=1), // player should play 6 seconds , stop. var done = false; function onplayerstatechange(event) { if (event.data == yt.playerstate.ended) { window.location = "callback:anything"; }; } function stopvideo() { player.stopvideo(); window.location = "callback:anything"; } function gettime() { return player.getcurrenttime(); } function getduration() { return player.getduration(); } function pause() { player.pausevideo(); } function play() { player.playvideo(); } </script> </body> </html> also have create uiwebview subclass delegate uiwebviewdelegate
- (id)initwithframe:(cgrect)frame { self = [super initwithframe:frame]; if (self == nil) return nil; self.mediaplaybackrequiresuseraction = no; self.delegate = self; self.allowsinlinemediaplayback = true; self.userinteractionenabled = false; return self; } - (void)loadvideo:(nsstring *)videoid { nsstring *filepath = [[nsbundle mainbundle] pathforresource:@"youtube" oftype:@"html"]; // if (filepath == nil) nserror *error; nsstring *string = [nsstring stringwithcontentsoffile:filepath encoding:nsutf8stringencoding error:&error]; // todo: error check string = [nsstring stringwithformat:string, videoid]; nsdata *htmldata = [string datausingencoding:nsutf8stringencoding]; // if (htmldata == nil) nsstring *documentsdirectorypath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0]; nsstring *targetpath = [documentsdirectorypath stringbyappendingpathcomponent:@"youtube2.html"]; [htmldata writetofile:targetpath atomically:yes]; [self loadrequest:[nsurlrequest requestwithurl:[nsurl fileurlwithpath:targetpath]]]; file = 0; } - (bool)webview:(uiwebview *)webview shouldstartloadwithrequest:(nsurlrequest *)request navigationtype:(uiwebviewnavigationtype)navigationtype { if ([[[request url] scheme] isequaltostring:@"callback"]) { playing = false; nsstring *documentsdirectorypath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0]; nsstring *targetpath = [documentsdirectorypath stringbyappendingpathcomponent:@"youtube2.html"]; nserror *error; [[nsfilemanager defaultmanager] removeitematpath:targetpath error:&error]; } return yes; } basically creates uiwebview , loads code in youtube.html file. because youtube.html static , need load id create copy on fly in documents folder youtube2.html in add string id.
the whole thing [self loadrequest:[nsurlrequest requestwithurl:[nsurl fileurlwithpath:targetpath]]]; works loading nsurlrequest string doesn't.
the javascript functions see in html file controlling video. if need access time or full duration can them this:
float videoduration = [[yt_webview stringbyevaluatingjavascriptfromstring:@"getduration()"] floatvalue]; float videotime = [[yt_webview stringbyevaluatingjavascriptfromstring:@"gettime()"] floatvalue];
Comments
Post a Comment