Skip to content

fix: resume AudioContext before playback to fix sounds on Android#13

Merged
xfalcox merged 1 commit into
mainfrom
copilot/fix-join-leave-sounds
Jun 24, 2026
Merged

fix: resume AudioContext before playback to fix sounds on Android#13
xfalcox merged 1 commit into
mainfrom
copilot/fix-join-leave-sounds

Conversation

@xfalcox

@xfalcox xfalcox commented Jun 24, 2026

Copy link
Copy Markdown
Member

On Android/mobile, new AudioContext() starts in suspended state per the Web Audio autoplay policy. Without an explicit resume(), no audio is produced — desktop browsers auto-resume, which is why the bug was mobile-only.

Changes

  • Shared AudioContext — replaced per-call new AudioContext() with a lazily-initialized module-level singleton (sharedCtx), avoiding browser limits on concurrent contexts (~6)
  • ctx.resume() before schedulinggetAudioContext() resumes the context if suspended before returning it, unblocking playback on mobile
  • Removed ctx.close() on onended — closing a shared context would break all subsequent sounds; nodes are disposed automatically by the GC after they finish
// Before: new context per sound, no resume, closed on ended
export function playUserJoinedSound() {
  const ctx = new AudioContext(); // suspended on Android
  const now = ctx.currentTime;   // 0, nothing schedules
  // ...
  osc.onended = () => ctx.close();
}

// After: shared context, resumed before use
export async function playUserJoinedSound() {
  const ctx = await getAudioContext(); // resumes if suspended
  const now = ctx.currentTime;
  // ...
}

@xfalcox xfalcox merged commit e7101f1 into main Jun 24, 2026
12 checks passed
@xfalcox xfalcox deleted the copilot/fix-join-leave-sounds branch June 24, 2026 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants