Created
May 22, 2018 17:54
-
-
Save greggnakamura/08d365f0c02b49cf282e3c5c5107a80e to your computer and use it in GitHub Desktop.
HTML: Video `autoplay` with `promise` and `muted` default
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
<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