Game audio in Flutter with flutter_soloud
flutterflutter-soloudgame-audioogglatencyweb-audiostutter
flutter_soloud (SoLoud C++ engine over FFI, miniaudio backend, WASM on web) is the audio
engine Run uses instead of flame_audio. This covers its tuning, the formats to use, web
quirks, and the hardest-won lesson: keep the music architecture simple.
Architecture lesson: one track at a time
Run's music system was rewritten twice. A priority-based event queue with suppression, slot
retirement and Future.delayed cleanup caused stutter, leaks and four rounds of fixes, because
every screen calling request/end created a distributed state machine that broke on each
navigation change. The replacement has no such bugs:
- playTrack(path, volume) replaces whatever is playing; stopTrack(); pauseTrack() /
resumeTrack() only for game pause.
- Each screen plays its own track on entry. Stop gameplay music explicitly before
Navigator.pop; do not rely on the game's onRemove() (it may not fire).
- Treat any proposal with priorities, queues, suppression or slot systems for music as
over-engineering.
- Volume: separate music and SFX multipliers from settings.
Run's layered-stem "vertical remixing" system (stems started together at volume 0 and faded by
a danger level) was eventually deleted as unused, along with ~12 MB of unreferenced music.
Latency and voices
- Typical SFX latency: Android AAudio (11+) 20-40 ms, OpenSL ES 40-80 ms, iOS under 10 ms.
- Default buffer 2048 samples at 44.1 kHz is ~46 ms; try
bufferSize: 1024inSoLoud.init()and test on device. - Load SFX with
LoadMode.memory(no decode latency), long music withLoadMode.disk; setautomaticCleanup: true. Preload all SFX at startup. - 16 active voices by default (1024 virtual); raise to 32 for stems
(
setMaxActiveVoiceCount) andsetProtectVoice(handle, true)on music. Over the limit, SoLoud plays the loudest and virtualizes the quietest. - For stems: identical length/BPM/sample rate, sample aligned, no pre-roll; start them all at
once and only fade volumes (
fadeVolume) to stay in sync forever. Switch environments at bar boundaries.
Formats
- OGG Vorbis for everything: gapless loops, no licensing, much smaller than WAV (converting WAV assets saved ~40 MB in Run).
- Avoid MP3 for loops: encoder padding leaves a gap at the loop point.
- WAV only during development.
- SFX: mono, 22.05 kHz, 96-128 kbps (half the data, ~90% perceived quality); music: stereo 44.1 kHz, 128-192 kbps (stems around quality 6). Mono SFX and OGG also save battery.
Web
- Autoplay policy: the AudioContext starts suspended; initialize/resume inside a user tap.
- The WASM worker needs
<script>tags inweb/index.html; initialization can race, so retry (5 attempts, 200 ms apart). loadFile()does not exist on web; useloadAsset()/loadMem().- Web throws generic exceptions, not
SoLoudException; use a barecatch (e). - WASM mixing costs more CPU; 4+ stems can crackle on low-end devices.
Mobile lifecycle
Use audio_session for Android audio focus; pause on background, resume on foreground. On iOS
AudioSession.setActive(false) can fail with AVAudioSessionErrorCodeIsBusy while SoLoud
holds sources (dispose sources first). Dispose the engine on AppLifecycleState.detached.
"Audio stutter" is usually frame time
SoLoud mixes on its own native thread and FFI play/stop calls are cheap. Stutter in Run came from main-thread frame spikes longer than the audio buffer (per-frame collision scans, liquid simulation rasterizing millions of rects). Profile frames before touching the audio code (Flutter / Flame canvas rendering performance facts).
Assets
Run's tracks are CC0 (Juhani Junkala, OpenGameArt, MintoDog, Cleyton Kauffman). Justin keeps
audio source files for import in ~/assets/audio/gameplay/; convert to OGG before adding.
Flutter's pubspec assets: does not recurse: list every subdirectory.
See also
- Adaptive game audio (mood crossfades and procedural sound)
- just_audio in Flutter, gotchas and patterns