Skip to content

Instantly share code, notes, and snippets.

@greggnakamura
Created May 22, 2018 17:54
Show Gist options
  • Save greggnakamura/08d365f0c02b49cf282e3c5c5107a80e to your computer and use it in GitHub Desktop.
Save greggnakamura/08d365f0c02b49cf282e3c5c5107a80e to your computer and use it in GitHub Desktop.
HTML: Video `autoplay` with `promise` and `muted` default
<video id="ouazPromo" muted autoplay>
<source src="https://www.ottawa.edu/OttawaU/media/OttawaSurprise/Video/OUAZPromo.mp4" type="video/mp4" />
<source src="https://www.ottawa.edu/OttawaU/media/OttawaSurprise/Video/OUAZPromo.webm" type="video/webm" />
<source src="https://www.ottawa.edu/OttawaU/media/OttawaSurprise/Video/OUAZPromo.ogv" type="video/ogg" />
</video>
<button id="unmuteButton" class="sr-only"></button>
<script>
var promise = document.getElementById('ouazPromo').play();
if (promise !== undefined) {
promise.then(_ => {
console.log('Autoplay started!');
}).catch(error => {
unmuteButton.addEventListener('click', function() {
video.muted = false;
});
});
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment