Reputation: 8309
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
Upvotes: 56
Views: 117317
Reputation: 1
To enable autoplay in the video, you need to include the "mute" parameter in the iframe's source. It's important to note that although autoplay is typically allowed by browsers like Firefox and Chrome, the video must begin without sound.
&mute=1
Upvotes: 0
Reputation: 346
UPDATE :
iOS 10+ now allows auto-play on HTML5 < video> elements, just have to mute the audio on elements. Youtube will still not. Android is still SOL too, but hey, its a start!
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/
Upvotes: 4
Reputation: 51
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]];
Upvotes: 0
Reputation: 8100
It can't be done. For various reasons (including, but not limited to data usage), Apple doesn't allow auto-playing of videos.
See the accepted answer to this question.
Upvotes: 76