Javascript YouTube 自动播放在 Chrome 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50217075/
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-23 04:34:25  来源:igfitidea点击:

YouTube autoplay not working in Chrome

javascriptgoogle-chromeyoutubeautoplay

提问by chechu

For a while now I have been using this script, where the video is automatically playing full-screen. For now suddenly the video doesn't play automatically in Chrome. But in Firefox and Edge it still works.

一段时间以来,我一直在使用此脚本,其中视频会自动全屏播放。现在突然视频不能在 Chrome 中自动播放。但是在 Firefox 和 Edge 中它仍然有效。

So maybe Google changed settings? Does anyone know how to fix this, please? You see the live example here: www.brunomazereel.com

那么也许谷歌改变了设置?请问有人知道怎么解决吗?您可以在此处查看现场示例:www.brunomazereel.com

<script src="http://www.youtube.com/player_api"></script>
<script type="text/javascript">
var player;
function onYouTubePlayerAPIReady() {
    player = new YT.Player('player', {
    playerVars: {
        'autoplay': 1,
        'controls': 0,
        'autohide': 1,
        'wmode': 'opaque',
        'showinfo': 0,
        'loop': 1,
        'rel': 0,
        'playlist': 'rh5QiehIlVA,Bl63bdR-Ko0,'
        },
    videoId: 'u-cjliof1xk',
    events: {
        'onReady': onPlayerReady
    }
});
}
function onPlayerReady(event) {
event.target();
$('#text').fadeIn(400);
}
$(window).scroll(function() {
var hT = $('.m-video').height(),
   wS = $(this).scrollTop();
if (wS > hT) {
  player.pauseVideo();
}
else {
  player.playVideo();
}
});
</script>

回答by Alexandre Beaudet

(One) of the possible solution taken from the comments discussion would be muting the videoif sound isn't that important in your case (if it is, I'll leave the answer as it could help other people).

(一)如果声音在您的情况下不那么重要(如果是,我会留下答案,因为它可以帮助其他人),从评论讨论中获得的可能解决方案之一是将视频静音

It's apparently the only way to have autoplay alwaysenabled. From the article :

这显然是始终启用自动播放的唯一方法。从文章:

"Muted autoplay is always allowed."

“始终允许静音自动播放。”

Source : Google changelog

来源:谷歌更新日志



Simply add in your playerVars:

只需在您的 playerVars 中添加:

mute : 1

Source for the muted video

静音视频的来源

回答by Serge

You should put the allow="autoplay"on the embedding iframeelement

你应该把allow="autoplay"放在嵌入iframe元素上

<!-- Autoplay is allowed. -->
<iframe src="https://cross-origin.com/myvideo.html" allow="autoplay">

<!-- Autoplay and Fullscreen are allowed. -->
<iframe src="https://cross-origin.com/myvideo.html" allow="autoplay; fullscreen">

Chrome's autoplay policies:

Chrome 的自动播放政策

  • Top frames can delegate autoplay permissionto their iframes to allow autoplaywith sound.
  • Mutedautoplay is always allowed.
  • Autoplay with soundis allowed if:
    • User has interacted with the domain (click, tap, etc.).
    • On desktop, the user's Media Engagement Indexthreshold has been crossed, meaning the user has previously play video with sound.
    • On mobile, the user has added the site to their home screen.
  • 顶级框架可以将自动播放权限委托给他们的iframes 以允许autoplay使用 sound
  • 始终允许静音自动播放。
  • 在以下情况下允许自动播放声音
    • 用户与域进行了交互(单击、点击等)。
    • 在桌面上,用户的媒体参与指数阈值已被超过,这意味着用户之前曾播放过有声视频。
    • 在移动设备上,用户已将该站点添加到其主屏幕。

回答by Marker

Google chrome removed the autoplay functionality. See below:

谷歌浏览器删除了自动播放功能。见下文:

https://developers.google.com/web/updates/2017/09/autoplay-policy-changes

https://developers.google.com/web/updates/2017/09/autoplay-policy-changes

回答by Alex

for me worked this solution:

对我来说,这个解决方案是:

jQuery("video-iframe")[0].src += "&autoplay=1";

It doesn't played for me even with all valid autoplay html attributes, but above code helped.

即使使用所有有效的自动播放 html 属性,它也不会为我播放,但上面的代码有所帮助。