Forked from cvan/aframe-autoplay-video-on-mobile.js
Created
October 25, 2017 20:08
-
-
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
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
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