Everyone who tried to make Youtube/Vimeo videos start play automatically on iOS knows that it could be a painful task. Apple blocked the 'autoplay' parameter for the right reasons, but sometimes you still need to get this functionality working.
I had the same issue with auto playing youtube videos, apparently, to get the autoplay to work, you need to do some javascript magic, listen for the player's 'onReady' event, than when the player is loaded and ready to play you call 'player.play()' to start it without any another user intervention.
Vimeo also got some javascript API, and I'm pretty sure you can do the autoplay trick with it, I just can't figure it up how to use it.
They have JS mini-library called Froogaloop
to make things easier, I saw this answer by @ila who use it in conjunction with the 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 this doesn't work for me, after adding alerts to this code, I can see that func1()
get called and executing the 'addEvent' line, BUT it seams that the ready()
method never gets called because the 'ready' event never fired (?)...
Any ideas?