'flutter AssetsAudioPlayer stop other instances

I'm working on a quiz app and if the user selects a correct answer the app should play a success sound and if the answer is wrong the sound should be another one (e.g please try again )

my problem is when the user taps on another answer the old player are already working I need to check if there is another instance of the player are playing and stop it

I'm using assets_audio_player: ^3.0.4+1

if (_correctAns != _selectedAns) {
  AssetsAudioPlayer.newPlayer().open(
    Audio("assets/audios/tryAgin.mp3"),
    showNotification: true,
  );
 
}
if (_correctAns == _selectedAns) {
  _assetsAudioPlayer.open(
    Audio("assets/audios/success.mp3"),
    showNotification: true,
  );
  Future.delayed(
    Duration(seconds: 2),
    () {
      nextQuestion();
    },
  );
  _numOfCorrectAns++;
}


Solution 1:[1]

I think u can use isPlaying to check if there is an audio playing .... and as another solution u can stop the current playing sound in onPressed then play the new sound ... if u have any question I can help u

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anas Saradar