19

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.

So maybe Google changed settings? Does anyone know how to fix this, please? You see the live example here: 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>
Alexandre Beaudet
  • 2,774
  • 2
  • 20
  • 29
chechu
  • 209
  • 1
  • 2
  • 8
  • 3
    Apparently, [Google did block autoplay](https://www.theverge.com/2018/5/3/17251104/google-chrome-66-autoplay-sound-videos-mute). Since it's built-in, I doubt there is a fix for that – Alexandre Beaudet May 07 '18 at 14:56
  • I see few errors at "event.target()" in the method "onPlayerReady". Although it might not be related ,by worth taking a look. Check the chrome console. – Dilip May 07 '18 at 14:58

4 Answers4

27

(One) of the possible solution taken from the comments discussion would be muting the video if 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 always enabled. From the article :

"Muted autoplay is always allowed."

Source : Google changelog


Simply add in your playerVars:

mute : 1

Source for the muted video

Alexandre Beaudet
  • 2,774
  • 2
  • 20
  • 29
  • About how to set youtube iframe mute: https://stackoverflow.com/questions/35044594/youtube-how-to-present-embed-video-with-sound-muted – larrykkk Dec 24 '20 at 02:19
12

You should put the allow="autoplay" on the embedding iframe element

<!-- 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 :

  • Top frames can delegate autoplay permission to their iframes to allow autoplay with sound.
  • Muted autoplay is always allowed.
  • Autoplay with sound is allowed if:
    • User has interacted with the domain (click, tap, etc.).
    • On desktop, the user's Media Engagement Index threshold has been crossed, meaning the user has previously play video with sound.
    • On mobile, the user has added the site to their home screen.
serge
  • 13,940
  • 35
  • 121
  • 205
8

Google chrome removed the autoplay functionality. See below:

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

Marker
  • 560
  • 4
  • 20
  • 1
    "Muted autoplay is always allowed", might be worth something – Alexandre Beaudet May 07 '18 at 14:59
  • 1
    Chrome's autoplay policies are simple: 1)Muted autoplay is always allowed. 2)Autoplay with sound is allowed if: User has interacted with the domain (click, tap, etc.). On desktop, the user's Media Engagement Index threshold has been crossed, meaning the user has previously play video with sound. On mobile, the user has added the site to his or her home screen. Top frames can delegate autoplay permission to their iframes to allow autoplay with sound. – Dilip May 07 '18 at 15:01
0

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.

Alex
  • 381
  • 4
  • 14