diff --git a/src/hub.js b/src/hub.js index 554d1f3a7a449f3fd22571b7a251c6690ee74b77..feb07c11faff1a700d1fe3262cbd32bf204f1729 100644 --- a/src/hub.js +++ b/src/hub.js @@ -17,6 +17,7 @@ import "aframe-billboard-component"; import "aframe-rounded"; import "webrtc-adapter"; import "aframe-slice9-component"; +import "./utils/ios-audio-context-fix"; import trackpad_dpad4 from "./behaviours/trackpad-dpad4"; import joystick_dpad4 from "./behaviours/joystick-dpad4"; diff --git a/src/utils/ios-audio-context-fix.js b/src/utils/ios-audio-context-fix.js new file mode 100644 index 0000000000000000000000000000000000000000..ce474194f663ae81040f8fdadcdb5a5130792a37 --- /dev/null +++ b/src/utils/ios-audio-context-fix.js @@ -0,0 +1,23 @@ +/** + * Mobile Safari will start Audio contexts in a "suspended" state. + * A user interaction (touch event) is needed in order to resume the AudioContext. + */ +const iDevices = /\biPhone.*Mobile|\biPod|\biPad|AppleCoreMedia/; + +if (iDevices.test(navigator.userAgent)) { + document.addEventListener("DOMContentLoaded", () => { + const ctx = THREE.AudioContext.getContext(); + + function resume() { + ctx.resume(); + + setTimeout(function() { + if (ctx.state === "running") { + document.body.removeEventListener("touchend", resume, false); + } + }, 0); + } + + document.body.addEventListener("touchend", resume, false); + }); +}