Background
Since Chrome version 66, videos that should autoplay on my site may be prevented from playing if the user haven't been on my site before.
<video src="..." autoplay></video>
Question
How do I detect if the video autoplay was disabled? And what can I do about it?
The
autoplay
attributeAccording to web standard specifications, the autoplay attribute should only be a hint for what the browser should to with the media element. Neither of W3 of WHATWG web specifications mentions anything about when to prevent autoplay for media, which means that each browser probably have different implementations.
Autoplay policies
Autoplay policies implemented by each browser now govern whether video should be allowed to autoplay.
Safari developers made a post on webkit.org regarding this.
Firefox seems to put it in the hands of the user to choose if it's allowed or not (link).
Best practices
I'll try to cover what the best practices are and what you can do in this section.
Detecting if autoplay is disabled
Instead of using
autoplay
on your element, you can use theplay()
method on thevideo
andaudio
element to start playing your media. Theplay()
method returns a promise in modern browsers (all according to the spec). If the promise rejects, it can indicate that autoplay is disabled in the current browser on your site.can-autoplay is a library solely for detecting autoplay features for both video and audio elements.
If autoplay is disabled
The good thing is that when you know that autoplay is disabled you can, in some browsers, then mute the video and try the
play()
method again, while showing something in the UI that says that the video is playing while muted.For me best solution was: