-1

I wanted to ask whether there actually is any way possible to auto play a video without the muted attribute? I thought it might be possible somehow since when you click on a YouTube video link, it auto plays. Thanks for your answers in advance~

Basically auto play video with sound when page loads, if I open a YouTube link it plays the video and audio as well, then it must be possible right?

  • What do you mean? Do you mean in an embeded code? Not sure what you are trying to achieve, do you want to autoplay on page load, but with sound? Usually that is standard, it's only muted when you place the mute attribute. For youtube you can check the guideline: https://support.google.com/youtube/answer/171780?hl=en#zippy=%2Cmake-an-embedded-video-play-automatically – Bart Apr 27 '23 at 09:27
  • Don't set the muted attribute. – Geshode Apr 27 '23 at 09:31
  • To clarify the legitimate question here: typically browsers block audio on auto-play, because it's annoying when sounds starts blaring from e.g. an embedded ad in a news article. That's an actual thing, and this question is legitimate. – deceze Apr 27 '23 at 09:32
  • Official guidance: https://developer.mozilla.org/en-US/docs/Web/Media/Autoplay_guide – deceze Apr 27 '23 at 09:34
  • My question is more of, how does youtube autoplays video with sound? – lifeisstrange Apr 27 '23 at 09:39
  • @Bart , deceze Thanks for the link! I'll check it out. – lifeisstrange Apr 27 '23 at 09:41

1 Answers1

2

Start by reading the official guidance: https://developer.mozilla.org/en-US/docs/Web/Media/Autoplay_guide for clarification.

The way YouTube does it is (I think) by having a SPA, i.e. a Javascript-based site. When you click on a thumbnail, it doesn't load a different page, it just changes the contents of the current page via Javascript. Thus the user has interacted with the current page and triggered the video playback through their action, allowing full audible playback.

Also note this point from the article:

As a general rule, you can assume that media will be allowed to autoplay [..] if the site has been allowlisted; this may happen [..] automatically if the browser determines that the user engages with media frequently [..]

I'm fairly sure I've had to click Play explicitly a few times on YouTube; possibly on a "new" device when I came to a video page from an external page. But once you have played a couple of videos on YouTube, it probably becomes allowlisted implicitly. I'm pretty sure that clause exists specifically for YouTube and similar video sites so as not to undermine their business model.

If you load a new page which contains an audible <video autoplay ...> element, that will generally be blocked, and there isn't really anything you can do about it.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • 1
    Though this explanation does not actually explain how they do it when you directly go to a video page from, say, Google search results. Then the SPA explanation obviously doesn't hold. – deceze Apr 27 '23 at 09:44
  • Yeah, it still auto plays with audio even when the video link is directly visited, and if you see in dom they too have a normal ` – lifeisstrange Apr 27 '23 at 09:48
  • 1
    I'm not sure if this is simply covered by *"if the site has been allowlisted; this may happen automatically if the browser determines that the user engages with media frequently"* from the article. I _have_ had times where I needed to click play explicitly on YouTube. It's entirely possible that on a completely blank device, you do need to click Play the first couple of times, and then it simply gets whitelisted. None of us notice this anymore, because we're all obsessively on YouTube and it's been whitelisted for all of us anyway. – deceze Apr 27 '23 at 09:54
  • Checking in my browser, YouTube is definitely allowlisted, and _I_ haven't done it explicitly. – deceze Apr 27 '23 at 10:01
  • If I am understanding correct, you mean that when we click play on video we visit the browsers remember this behaviour and whitelists it? I am not sure, I just tried opening a YouTube link in edge which I just used to install chrome and nothing else, but it still auto played with sound, however when I opened the video link on my mobile it was muted. I am confused. Nonetheless, thanks for your help! – lifeisstrange Apr 27 '23 at 10:19
  • 1
    I wouldn't be surprised if certain sites are simply allowlisted by default in browsers. You can be pretty sure YouTube is allowlisted in Chrome for example. – deceze Apr 27 '23 at 10:29