Search code examples
javascriptjqueryyoutubehtml5-videoautoplay

How does YouTube work?


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)?


Solution

  • 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.