Last active
October 24, 2023 11:11
-
-
Save HereOrCode/ee914fc69f44678384fcf6d1c396cf46 to your computer and use it in GitHub Desktop.
Video auto play
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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