Youtube embedded video: autoplay feature not working in iphone
HtmlYoutubeAutoplayHtml Problem Overview
I have a youtube embeded video link in HTML5 page, which I want to autoplay.
The following code works in browsers, but in iphone; its not working and needs an extra click.
<iframe type="text/html" width="125" height="100" src="http://www.youtube.com/embed/d_g0251EfB8?autoplay=1" frameborder="0"></iframe>
what to do
Html Solutions
Solution 1 - Html
It can't be done. For various reasons (including, but not limited to data usage), Apple doesn't allow auto-playing of videos.
Solution 2 - Html
UPDATE :
iOS 10+ now allows auto-play on HTML5 < video> elements, just have to mute the audio on
SAMPLE:
<video autoplay muted>
<source src="movie.mp4" type="video/mp4">
Sadly, your browser does not support the video tag X_x
</video>
INFO SOURCE: https://webkit.org/blog/6784/new-video-policies-for-ios/
Solution 3 - Html
I have tried with following and Youtube video successfully autoplays in fullscreen when web view finish loading:
[self.webView setAllowsInlineMediaPlayback:YES];
[self.webView setMediaPlaybackRequiresUserAction:NO];
[self.view addSubview:self.webView];
NSString* embedHTML = [NSString stringWithFormat:@"\
<html>\
<body style='margin:0px;padding:0px;'>\
<script type='text/javascript' src='http://www.youtube.com/iframe_api'></script>\
<script type='text/javascript'>\
function onYouTubeIframeAPIReady()\
{\
ytplayer=new YT.Player('playerId',{events:{onReady:onPlayerReady}})\
}\
function onPlayerReady(a)\
{ \
a.target.playVideo(); \
}\
</script>\
<iframe id='playerId' type='text/html' width='100%%' height='%f' src='http://www.youtube.com/embed/%@?enablejsapi=1&rel=0&playsinline=0&autoplay=1' frameborder='0'allowfullscreen>\
</body>\
</html>",self.webView.frame.size.height,@"Dw9jFO_coww"];
[self.webView bringSubviewToFront:self.btnBack];
self.webView.backgroundColor = [UIColor clearColor];
self.webView.opaque = NO;
[self.webView loadHTMLString:embedHTML baseURL:[[NSBundle mainBundle] resourceURL]];