I want to play a short .wav sound every second and update a counter on the screen at the same time.
I'm using the flutter_soloud package and based my code on their metronome example from GitHub.
However, there's a noticeable delay (more than 250ms) between the UI update and the sound actually playing, even though I'm using:
bufferSize: 256inSoLoud.init()- very short
.wavfiles (mono, 44.1kHz)
Here’s a simplified version of my current code:
void initState() {
super.initState();
SoLoud.instance.loadAsset('assets/sounds/1.wav').then((value) async {
tick1 = value;
tick1Handle = await SoLoud.instance.play(tick1!, paused: true);
});
}
void start() {
timer?.cancel();
timer = Timer.periodic(const Duration(seconds: 1), (_) {
if (tick1 != null) {
SoLoud.instance.play(tick1!);
}
setState(() {
count++;
});
});
}
I also experimented with adding a Future.delayed(Duration(milliseconds: 250)) before calling setState() to delay the UI update and make it appear synchronized with the audio.
This kind of works visually — the counter appears more in sync with the sound — but it feels like a hack, and I’m not confident it would behave consistently across all devices or frame rates.
Is there a better way to accurately sync audio and UI in Flutter? I tried another packages like just_audio and audioPlayers, but with same result.