Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jshaw/608a353ee71d5971973f89c4d717dd4c to your computer and use it in GitHub Desktop.
Save jshaw/608a353ee71d5971973f89c4d717dd4c to your computer and use it in GitHub Desktop.
handle autoplay of <video> in A-Frame on mobile (iOS, Android) - see https://github.com/aframevr/aframe/pull/2657/files
function playVideoOnClick (selector) {
el = document.querySelector(selector);
if (el) {
addListener();
} else {
window.addEventListener('load', addListener);
}
function addListener () {
if (el.paused) {
window.addEventListener('click', handleFirstClick);
}
function handleFirstClick () {
try {
if (el.muted) {
delete el.muted;
}
if (el.paused) {
el.play();
}
} catch (e) {
}
removeHandleFirstClick();
}
function removeHandleFirstClick () {
window.removeEventListener('click', handleFirstClick);
}
}
}
playVideoOnClick('video');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment