Youtube embedded video: autoplay feature not working in iphone
Asked Answered
H

4

56

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

Homoousian answered 15/11, 2011 at 19:1 Comment(0)
F
76

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.

Fiddlefaddle answered 15/11, 2011 at 19:44 Comment(5)
thnaks. let me delete my questionHomoousian
@AvisekChakraborty: Please don't delete it. This is still useful information, even if the answer is not what you had hoped for.Stumpage
So how come than facebook and instagram does it?Culinary
beware that the YouTube iFrame Player API playVideo() function doesnt work on mobile if you think that is a solution :(Kensell
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."Rehearse
P
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/

Peters answered 2/6, 2017 at 18:15 Comment(0)
Z
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]];
Zuzana answered 30/3, 2016 at 6:37 Comment(2)
code is filled with typos and lacks clarity :( oh its bad.Kensell
OP was asking for HTLM5 not ObjC, no?Employer
S
0

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

Septuor answered 12/10, 2023 at 7:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.