flutterjust-audioflame

just_audio in flutter need many instances to avoid Player InterruptedException (Connection aborted)


If I use just one AudioPlayer and try to play sound effects for my game characters using the just_audio: ^0.9.35 flutter plugin I get Player InterruptedException.

To avoid that I had to create 6 AudioPlayer instances each representing my game characters. See the following code.

My question: is there a better way to implement this? Is this going to create performance issues?

static Future<void> playSfx(String file,{double volume = 1}) async {
  try {
    await currentPlayer.setAsset('assets/audio/$file');
    await currentPlayer.setVolume(volume);
    await currentPlayer.play();
  } catch (e) {
}}

Solution

  • In just_audio, the correct way to play N sounds at the same time is to create N instances of the player.

    However, there is a limit to how many player instances you can run in parallel. That limit is not defined as it's going to depend on the resources available on the particular device. Smaller/older devices will have more limited resources. To find that limit, you would need to do your own testing.

    Since each player uses a lot of resources, be sure to dispose of your player instances once you're finished with them (i.e. not right after playing the sound, but perhaps when the user closes your app, or the game is over.)