fix: prime iOS WebKit audio pipeline with silent oscillator on first touch
This commit is contained in:
@@ -193,10 +193,23 @@ const SFX = (() => {
|
|||||||
return ctx;
|
return ctx;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function primer() {
|
||||||
|
if (!ctx) ctx = new (window.AudioContext || window.webkitAudioContext)();
|
||||||
|
ctx.resume().then(() => {
|
||||||
|
const osc = ctx.createOscillator();
|
||||||
|
const gain = ctx.createGain();
|
||||||
|
osc.connect(gain);
|
||||||
|
gain.connect(ctx.destination);
|
||||||
|
gain.gain.setValueAtTime(0.0001, ctx.currentTime);
|
||||||
|
osc.start(ctx.currentTime);
|
||||||
|
osc.stop(ctx.currentTime + 0.001);
|
||||||
|
}).catch(() => {});
|
||||||
|
}
|
||||||
function unlock() {
|
function unlock() {
|
||||||
if (!ctx) ctx = new (window.AudioContext || window.webkitAudioContext)();
|
if (!ctx) ctx = new (window.AudioContext || window.webkitAudioContext)();
|
||||||
if (ctx.state === 'suspended') ctx.resume().catch(() => {});
|
if (ctx.state === 'suspended') ctx.resume().catch(() => {});
|
||||||
}
|
}
|
||||||
|
document.addEventListener('touchstart', primer, { once: true, passive: true });
|
||||||
document.addEventListener('touchstart', unlock, { passive: true });
|
document.addEventListener('touchstart', unlock, { passive: true });
|
||||||
document.addEventListener('touchend', unlock, { passive: true });
|
document.addEventListener('touchend', unlock, { passive: true });
|
||||||
document.addEventListener('pointerdown', unlock, { passive: true });
|
document.addEventListener('pointerdown', unlock, { passive: true });
|
||||||
|
|||||||
Reference in New Issue
Block a user