javascript - Objective c - How to autoplay Vimeo videos using JS -
everyone tried make youtube/vimeo videos start play automatically on ios knows painful task. apple blocked 'autoplay' parameter right reasons, still need functionality working.
i had same issue auto playing youtube videos, apparently, autoplay work, need javascript magic, listen player's 'onready' event, when player loaded , ready play call 'player.play()' start without user intervention.
vimeo got javascript api, , i'm pretty sure can autoplay trick it, can't figure how use it.
they have js mini-library called froogaloop
make things easier, saw this answer @ila use in conjunction following html string:
nsstring *htmlstring = [nsstring stringwithformat: @"<html><head>" "<script src=\"froogaloop.js\"></script>" " <script>" "function ready()" "{$f(document.getelementbyid(\"player\")).api(\"play\");}" "function func1() " "{$f(document.getelementbyid(\"player\")).addevent(\"ready\", ready);}" "window.onload=func1;" "</script></head><body>" "<iframe id=\"player\" src=\"http://player.vimeo.com/video/39287488?api=1&player_id=player\" width=\"315\" height=\"455\" frameborder=\"0\" webkit-playsinline>" " </iframe></body></html>"]; - (void)webviewdidfinishload:(uiwebview *)webview { nslog(@"webview loaded"); nsstring *path = [[nsbundle mainbundle] pathforresource:@"froogaloop" oftype:@"js"]; nsstring *jscode = [nsstring stringwithcontentsoffile:path encoding:nsutf8stringencoding error:nil]; [webview stringbyevaluatingjavascriptfromstring:jscode]; }
but doesn't work me, after adding alerts code, can see func1()
called , executing 'addevent' line, but seams ready()
method never gets called because 'ready' event never fired (?)...
any ideas?
my first answer question less ideal brute force method. i'd suggest looking @ second method using vimeo javascript api , froogaloop.
i'm going assume you're using vimeo iframe inside html page , you're using page inside uiwebview inside ios app or inside safari.
a little bit on introspection vimeo iframe shows doesn't load html video tag initially. you're going have programatically tap 'play' button load video.
in order today (april 2013, may change) can grab right elements , call right functions. it's best demonstrated working sample code.
// loading page inside uiwebview <html> <head></head> <body> <iframe width="" height="" src="http://player.vimeo.com/video/62207569 " frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe> <script> // add listener window's load event window.addeventlistener('load', onwindowload, false); function onwindowload(event) { //use loop continue checking vimeo iframe 'play' button checkfortrue(isiframebuttonloaded, iframebuttonloaded); } function checkfortrue(func, callback) { settimeout(function() { if (func()) { callback( iframebutton() ); } else { checkfortrue(func, callback); }; }, 1000); } //finds 'play' button within vimeo iframe function iframebutton() { //window.frames[0].document.getelementsbytagname('div')[1]; // works... return window.frames[0].document.getelementsbytagname('button')[5]; } //simple boolean condition check iframe button function isiframebuttonloaded() { return ( iframebutton() != null ); } //once button loaded, click function iframebuttonloaded(iframebutton) { iframebutton.click(); } </script> </body> </html>
this source code available gist
a sane person may not best way it, purpose of automatically playing vimeo video within ios app seem work.
the code load html file bundle , load uiwebview boilerplate:
- (void)viewdidload { [super viewdidload]; nsstring *htmlfile = [[nsbundle mainbundle] pathforresource:@"vimeotrial" oftype:@"html"]; nsstring *htmlstring = [nsstring stringwithcontentsoffile:htmlfile encoding:nsutf8stringencoding error:nil]; [self.webview loadhtmlstring:htmlstring baseurl:nil]; }
Comments
Post a Comment