From 3ec20a8754d6face9b08b21ebd659e4d99025108 Mon Sep 17 00:00:00 2001 From: Robert Long <robert@robertlong.me> Date: Thu, 26 Apr 2018 10:22:23 -0700 Subject: [PATCH] Add iOS audio context fix --- src/hub.js | 1 + src/utils/ios-audio-context-fix.js | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 src/utils/ios-audio-context-fix.js diff --git a/src/hub.js b/src/hub.js index 554d1f3a7..feb07c11f 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 000000000..ce474194f --- /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); + }); +} -- GitLab