-9

Google Chrome doesn't respect the autoplay attribute on HTML5 <video> elements. Using video.play() JavaScript as soon as the page loads throws an error:

Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first.

OK, but YouTube videos are HTML5 and they autoplay just fine. What technique does YouTube use to bypass Chrome's restriction? Are all webpages able to do the same (or no, maybe because YouTube has an exception since Google owns it)?

clickbait
  • 2,818
  • 1
  • 25
  • 61
  • Possible duplicate of [How to make javascript alarm to be played 100%?](https://stackoverflow.com/questions/51529440/how-to-make-javascript-alarm-to-be-played-100) – Louys Patrice Bessette Aug 18 '18 at 03:38
  • @LouysPatriceBessette No, I am asking specifically about the JavaScript that YouTube uses to autoplay videos. – clickbait Aug 18 '18 at 03:39
  • I marked as duplicate because I answered to a similar question already. [**This Google article**](https://developers.google.com/web/updates/2016/03/play-returns-promise) hold all the information you seek. It's a browser limitation... Nothing to do with Youtube. – Louys Patrice Bessette Aug 18 '18 at 03:39
  • @LouysPatriceBessette Yeah but what exactly does YouTube do in the `Promise` to bypass this browser limitation? – clickbait Aug 18 '18 at 03:42
  • 1
    Youtube is owned by Google... I suppose they made an exception... The user definitely wants to see a video, which is not obvious on other sites. – Louys Patrice Bessette Aug 18 '18 at 03:44
  • This said... As in the suggested duplicate, you just have to trigger the play on any `body` click. ;) – Louys Patrice Bessette Aug 18 '18 at 03:48

1 Answers1

7

For Youtube, it is probably a pre-populated white list

https://blog.google/products/chrome/improving-autoplay-chrome/

If you don’t have browsing history, Chrome allows autoplay for over 1,000 sites where we see that the highest percentage of visitors play media with sound.


For other cases, you can refer to the following rules (for Chrome at least)

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

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 played video with sound.

Saxo_Broko
  • 378
  • 2
  • 15