Html Youtube 嵌入视频:自动播放功能在 iphone 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8141652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:31:34  来源:igfitidea点击:

Youtube embedded video: autoplay feature not working in iphone

htmlyoutubeautoplay

提问by Avisek Chakraborty

I have a youtube embeded video link in HTML5 page, which I want to autoplay.

我在 HTML5 页面中有一个 youtube 嵌入的视频链接,我想自动播放

The following code works in browsers, but in iphone; its not working and needs an extra click.

以下代码适用于浏览器,但适用于 iphone;它不工作,需要额外的点击。

<iframe type="text/html" width="125" height="100" src="http://www.youtube.com/embed/d_g0251EfB8?autoplay=1" frameborder="0"></iframe>

what to do

该怎么办

回答by Mike

It can't be done. For various reasons (including, but not limited to data usage), Apple doesn't allow auto-playing of videos.

做不到。出于各种原因(包括但不限于数据使用),Apple 不允许自动播放视频。

See the accepted answer to this question.

请参阅此问题的已接受答案

回答by Apurv Soni

I have tried with following and Youtube video successfully autoplays in fullscreen when web view finish loading:

我尝试了以下操作,当 Web 视图完成加载时,Youtube 视频成功地以全屏方式自动播放:

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

回答by Lux.Capacitor

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!

iOS 10+ 现在允许在 HTML5 <video> 元素上自动播放,只需要将元素上的音频静音。Youtube 仍然不会。Android 仍然是 SOL,但是嘿,这是一个开始!

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 : //webkit.org/blog/6784/new-video-policies-for-ios/