55

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

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
Avisek Chakraborty
  • 8,229
  • 10
  • 48
  • 76

3 Answers3

75

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.

Community
  • 1
  • 1
Mike
  • 7,994
  • 5
  • 35
  • 44
  • thnaks. let me delete my question – Avisek Chakraborty Nov 15 '11 at 21:43
  • 43
    @AvisekChakraborty: Please don't delete it. This is still useful information, even if the answer is not what you had hoped for. – Ed S. Jan 10 '12 at 19:31
  • So how come than facebook and instagram does it? – thibaut noah Jul 26 '16 at 15:09
  • 2
    beware that the YouTube iFrame Player API playVideo() function doesnt work on mobile if you think that is a solution :( – David Dec 04 '16 at 19:03
  • This sucks, actually. Preventing misuse of autoplay shouldn't shut down all of the other (and better) possibilities of a programmatic API. Sanity would be a user opt-in to allow programmatic control on a per-page or even per-visit basis, not just a draconian "ban them all." – Mallory-Erik Sep 21 '21 at 10:02
4

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/

Lux.Capacitor
  • 336
  • 3
  • 12
0

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]];
Apurv Soni
  • 51
  • 1
  • 5