Skip to content

Instantly share code, notes, and snippets.

@HereOrCode
Last active October 24, 2023 11:11
Show Gist options
  • Save HereOrCode/ee914fc69f44678384fcf6d1c396cf46 to your computer and use it in GitHub Desktop.
Save HereOrCode/ee914fc69f44678384fcf6d1c396cf46 to your computer and use it in GitHub Desktop.
Video auto play
/*
* Autoplay policy in Chrome - Chrome for Developers
* https://developer.chrome.com/blog/autoplay/#example_scenarios
*
* Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first.
* https://stackoverflow.com/a/68128950/7738653
*/
const video = document.querySelector("video");
if (!video) return;
const promise = video.play();
if (promise !== undefined) {
promise.then(() => {
// Autoplay started
}).catch(error => {
// Autoplay was prevented.
video.muted = true;
video.play();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment