@@ -13,34 +13,51 @@ function getContext() { |
| 13 | 13 | // Must be called on first user interaction to enable audio on mobile |
| 14 | 14 | export function unlockAudio() { |
| 15 | 15 | if (unlocked) return |
| 16 | + unlocked = true // Set immediately to prevent multiple attempts |
| 16 | 17 | |
| 17 | | - const ctx = getContext() |
| 18 | + // Create context fresh during user gesture (mobile requirement) |
| 19 | + if (!audioContext) { |
| 20 | + audioContext = new (window.AudioContext || window.webkitAudioContext)() |
| 21 | + } |
| 22 | + |
| 23 | + const ctx = audioContext |
| 18 | 24 | |
| 19 | | - // Resume if suspended |
| 25 | + // Resume synchronously - don't wait for promise |
| 20 | 26 | if (ctx.state === 'suspended') { |
| 21 | 27 | ctx.resume() |
| 22 | 28 | } |
| 23 | 29 | |
| 24 | | - // Play a silent buffer to fully unlock on iOS/mobile |
| 25 | | - const silentBuffer = ctx.createBuffer(1, 1, ctx.sampleRate) |
| 26 | | - const source = ctx.createBufferSource() |
| 27 | | - source.buffer = silentBuffer |
| 28 | | - source.connect(ctx.destination) |
| 29 | | - source.start(0) |
| 30 | + // Immediately play a sound to force audio pipeline open |
| 31 | + // Must happen synchronously in the user gesture |
| 32 | + try { |
| 33 | + const osc = ctx.createOscillator() |
| 34 | + const gain = ctx.createGain() |
| 35 | + |
| 36 | + osc.type = 'triangle' |
| 37 | + osc.frequency.value = 200 |
| 38 | + gain.gain.value = 0.001 // Nearly silent |
| 30 | 39 | |
| 31 | | - unlocked = true |
| 40 | + osc.connect(gain) |
| 41 | + gain.connect(ctx.destination) |
| 42 | + |
| 43 | + osc.start(0) |
| 44 | + osc.stop(ctx.currentTime + 0.1) |
| 45 | + } catch (e) { |
| 46 | + console.warn('Unlock sound failed:', e) |
| 47 | + } |
| 32 | 48 | } |
| 33 | 49 | |
| 34 | 50 | // Damp crunch sound - wet bread being chomped |
| 35 | 51 | export function playMonch() { |
| 36 | 52 | const ctx = getContext() |
| 37 | | - const now = ctx.currentTime |
| 38 | 53 | |
| 39 | | - // Resume context if suspended (browser autoplay policy) |
| 54 | + // Try to resume if needed, but don't block |
| 40 | 55 | if (ctx.state === 'suspended') { |
| 41 | 56 | ctx.resume() |
| 42 | 57 | } |
| 43 | 58 | |
| 59 | + const now = ctx.currentTime |
| 60 | + |
| 44 | 61 | // Master output - keep it gentle |
| 45 | 62 | const master = ctx.createGain() |
| 46 | 63 | master.gain.value = 0.25 |