// iOS silent-mode unlock + resume handling. import { getCtx } from './context'; type SessionNav = Navigator & { audioSession?: { type: string } }; let silent: HTMLAudioElement | null = null; let listening = false; function ensureSilentAudio(): void { if (!silent) { silent = document.createElement('audio'); silent.src = './audio/silence.mp3'; silent.loop = true; silent.preload = 'auto'; silent.setAttribute('playsinline', ''); silent.setAttribute('webkit-playsinline', ''); } if (silent.paused) void silent.play().catch(() => undefined); } export function iosUnlock(): void { try { const nav = navigator as SessionNav; if (nav.audioSession) nav.audioSession.type = 'playback'; else ensureSilentAudio(); } catch { /* ignore */ } const c = getCtx(); if (c && c.state !== 'running') { try { void c.resume().catch(() => undefined); } catch { /* ignore */ } } } /** Play a one-sample silent buffer (helps older iOS start the context). */ export function primeContext(): void { const c = getCtx(); if (!c) return; try { const s = c.createBufferSource(); s.buffer = c.createBuffer(1, 1, 22050); s.connect(c.destination); s.start(0); } catch { /* ignore */ } } export function installUnlockListeners(): void { if (listening) return; listening = true; const handler = (): void => { const c = getCtx(); if (!c || c.state !== 'running') iosUnlock(); }; const opts: AddEventListenerOptions = { capture: true, passive: true }; for (const ev of ['pointerdown', 'touchend', 'click', 'keydown']) window.addEventListener(ev, handler, opts); document.addEventListener('visibilitychange', () => { if (!document.hidden) handler(); }, opts); }