Flutter Audioplayers delay
Solution 1
I solve similar problem by having singleton class, and after first play I can get the state, and I can stop previous play.
class Audio {
static final playerCache = AudioCache();
static AudioPlayer audioPlayer;
static void play(String audioName) async {
audioPlayer = await playerCache.play('$audioName.mp3');
}
static void stop() {
audioPlayer.stop();
}
}
...
child: IconButton(
onPressed: () {
try {
if (Audio.audioPlayer.state ==
AudioPlayerState.PLAYING) {
Audio.stop();
} else {
Audio.play('bid');
}
} catch (e) {
Audio.play('bid');
}
},
...
Solution 2
There is a line of code in its own documentation.
To use the low latency API, better for gaming sounds, use:
AudioPlayer audioPlayer = AudioPlayer(mode: PlayerMode.LOW_LATENCY);
In this mode the backend won't fire any duration or position updates. Also, it is not possible to use the seek method to set the audio a specific position. This mode is also not available on web.
I hope it is useful.
midi
I love programming since my youth and have designed and realized various professional projects during my studies and afterwards. I love challenges and like to find elegant and modern solutions for technical problems that people like to use.
Updated on December 16, 2022Comments
-
midi over 1 year
I'm coding a small game with the Flutter Framework. I'm using audioplayers for the Sounds.
It works fine like this, when calling it for example 2 times a second. But whenn I call it more than 5 times and again in the next second at some point the sound has like a delay and then after a second or so all the sounds play at once :) That sounds weired.
I also tested the audioplayers example from github on my iphone. Repeating the sounds in low frequency is ok, but when I repeat clicking the button as fast as possible at some point it gets some delay and the same thing is happening.
Is there some way to stop the previous Sound before and then playing the next one or isnt this possible?
Or is there some other Idea how to deal with the sounds?
This is how I use it:
AudioCache upgradeSound = new AudioCache(); void playUpgradeSound() { _playUpgradeSound(upgradeSound); } void _playUpgradeSound(AudioCache ac) async{ await ac.play('audio/upgr.mp3'); }
Thank you very much in advance
-
Sagar Acharya over 4 yearshave you got any solution for that?
-
Sreekuttan about 4 yearsSame situation for me.
-
-
Paul Sumpner about 2 yearsUnrelated to this question, I replaced my seek(0) with stop(), resume() and it seems to work. Do you think that's that the best way to restart an already playing sample?