Audio in Browser Games: Sound, Music, and Web Audio API
Audio transforms a browser game from "tech demo" to "real game." But browser audio has unique challenges — autoplay policies, format compatibility, and performance gotchas that don't exist in native game development.
The Web Audio API: What You Need to Know
The Web Audio API gives you low-latency, programmable audio in the browser. It uses a node-based graph system: audio sources connect through processing nodes (gain, filter, panner) to the final output.
For games, you mainly need three things:
- AudioContext: The main audio engine — create one when the player first interacts with the page
- AudioBuffer: Pre-loaded sound data ready for instant playback
- GainNode: Volume control for individual sounds and master volume
Sound Effects: The Audio Sprite Pattern
Loading 50 individual sound files creates 50 HTTP requests. Instead, combine all sounds into a single audio file (an "audio sprite") and play specific segments by offset and duration.
This is identical to the sprite sheet concept for graphics — one file, multiple assets, indexed by position.
Best Practices for Sound Effects
- Pre-load during a loading screen, not on demand. First-play latency ruins the experience
- Use multiple instances of the same sound for rapid-fire effects (shooting, collecting). Create 3-4 AudioBufferSourceNodes from the same buffer and cycle through them
- Vary pitch slightly (±5-10%) each play to avoid robotic repetition
- Keep effects short — under 2 seconds for most game sounds. Use OGG format for best compression/quality balance
Music: Looping and Crossfading
Background music needs seamless looping. The Audio API's loop property works, but you'll hear a click at the loop point unless the audio file is specifically prepared:
- Export music with the loop point at a zero crossing (where the waveform crosses zero amplitude)
- Or use two music tracks and crossfade between them for seamless transitions
- Layer separate tracks (drums, melody, bass) and fade layers in/out for dynamic music that responds to gameplay
Common Pitfalls
The AudioContext Limit
Browsers limit the number of active AudioContexts (usually 6). Create ONE AudioContext for your entire game — don't create new ones per sound.
Memory Leaks
AudioBufferSourceNodes are single-use: once played, they can't be replayed. But they also don't get garbage collected if you keep references. Create them fresh for each play, and let them go out of scope after playback ends.
Format Compatibility
- MP3: Universal support but lossy compression adds artifacts to short sounds
- OGG Vorbis: Better quality per byte, supported everywhere except Safari (which added support in 2023)
- WebM (Opus): Best compression, excellent quality, broad support. Recommended default in 2026
- WAV: Lossless but huge files. Use only for development/testing
Ship OGG/WebM with an MP3 fallback for maximum compatibility.
Libraries That Simplify Everything
- Howler.js: The standard audio library for browser games. Handles formats, sprites, spatial audio, and mobile quirks. If you use one library, use this
- Tone.js: For procedural/generative audio — synthesizers, sequencers, effects. Overkill for most games but amazing for musical games
- FMOD for Web: The professional game audio middleware now has a WASM build. For complex audio systems with hundreds of sounds and adaptive music