NotAllowedError: play() failed because the user didn't interact with the document first
how to autoplay music when the browser loads, without user interaction
6 Replies
you do it by ... not
please
if you want to see something faster than light, just play music on a tab
the sheer speed at which it is closed is face meltingly insane
but on a serious note, use an audio tag instead of trying to create the elements in js
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/audio#autoplay
use that attribute, and you can have your audience annoyed by your site
The browser purposely prevents that so there’s no way around it. So please, just don’t.
Youtube is the only site that was granted special permission to do this. All other sites in existence are not allowed. It had been allowed in the past, but this ability was abused by advertisers. That was so annoying that the rule was created to never allow it again, but Youtube was able to get a special exception because it is owned by Google, and Google owns Chrome, so they had enough leverage to also get that exception accepted by other browsers.
the browser prevents the .play method if it isnt inside a trusted event listener triggered by an user action (click, keypress and such)
Do you have a source for this claim? I don't think that YT got a pass on this. The entire site is a SPA so it got 'permission' the first time the user clicks on anything, so there's no actual page changes or whatnot that could "reset" the permissions.
That's how it's able to keep the picture-in-picture while browsing different "pages". And thus also get around needing user input all the time.
I don't recall the source, but Youtube doesn't make use of this directly on their site. What they are able to do is autoplay an embedded video that isn't muted when you embed on another site. I haven't tested recently, but it was working as recently as last year on both Firefox and Chrome.