Kedar Karki
Kedar Karki

Reputation: 548

Play single audio at a time using audioplayers package

I have 18 audios on the same page. I am using the audio player's package for playing the audio. But when I play another audio, Both audios get played simultaneously. There are 18 instances of the audio player with different player id. How can I stop all other audios and play only one when the play button is fired?

Upvotes: 1

Views: 1740

Answers (1)

Morez
Morez

Reputation: 2229

You can pass the same Id to your audioPlayer so then you only have one unique audioPlayer. Before playing a file, Stop the audioPlayer if it’s already playing and then start it with the new file.

From documentation:

playerId

By default, each time you initialize a new instance of AudioPlayer a unique playerId is generated and assigned using uuid package, this is designed this way to play multiple audio files simultaneously, if you want to play using the same instance that was created before simply pass your playerId when creating a new AudioPlayer instance.

final audioPlayer = AudioPlayer(playerId: 'my_unique_playerId');

Upvotes: 1

Related Questions