我在 HTML5 页面中有一个 youtube 嵌入视频链接,我想自动播放。
以下代码适用于浏览器,但适用于 iphone;它不起作用,需要额外的点击。
<iframe type="text/html" width="125" height="100" src="http://www.youtube.com/embed/d_g0251EfB8?autoplay=1" frameborder="0"></iframe>
该怎么办
这是不可能的。由于各种原因(包括但不限于数据使用),Apple 不允许自动播放视频。
请参阅此问题的已接受答案。
更新 :
iOS 10+ 现在允许在 HTML5 <video> 元素上自动播放,只需将元素上的音频静音。Youtube 仍然不会。Android 仍然是 SOL,但是,嘿,它是一个开始!
样本:
<video autoplay muted>
<source src="movie.mp4" type="video/mp4">
Sadly, your browser does not support the video tag X_x
</video>
信息来源: https ://webkit.org/blog/6784/new-video-policies-for-ios/
当网络视图完成加载时,我尝试了以下和 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]];