Youtube embedded video: autoplay feature not working in iphone

若如初见. 提交于 2019-11-26 22:13:13

问题


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


回答1:


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.




回答2:


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]];



回答3:


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/



来源:https://stackoverflow.com/questions/8141652/youtube-embedded-video-autoplay-feature-not-working-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!