Last active
September 5, 2018 21:52
-
-
Save briankelleher/8e50d92b1897b35e1b0db2f9e31bd79c to your computer and use it in GitHub Desktop.
YouTube Style Autoplay Mute (Chrome UX Solution) - VueJS
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
// Semi-pseudo code below | |
<template> | |
<video :muted="muted"></video> | |
</template> | |
<script> | |
export default { | |
data: function() { | |
return { | |
muted: false | |
} | |
}, | |
methods: { | |
playVideo: function() { | |
let self = this | |
play = $(domelement).play() // Using jQuery for | |
if ( play !== undefined ) { | |
play.then( function() { | |
// Code can go here that runs when the video is definitively playing. | |
}) | |
.catch( function(error) { | |
if ( error.message.startsWith("play() failed because the user didn't interact with the document first.") | |
|| error.message.startsWith("The operation was aborted.") ) { | |
self.muted = true | |
self.$nextTick(function() { | |
play = domelement.play() | |
if ( play !== undefined ) { | |
play.then( function() { | |
// Code can go here that runs when the video is definitively playing. | |
}) | |
.catch( function(error) { | |
// Something went terribly wrong | |
}) | |
} | |
}) | |
} else { | |
// Something went kind of wrong | |
} | |
}) | |
} | |
} | |
}, | |
mounted() { | |
this.playVideo() | |
} | |
} | |
</script> | |
<style scoped> | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added support for safari - there should really be a standard error message or title so you can reliably handle.