Flutter-AssetsAudioPlayer icon indicating copy to clipboard operation
Flutter-AssetsAudioPlayer copied to clipboard

Two audio tracks play simultaneously when I close and reopen the app

Open MazenCoder opened this issue 1 year ago • 1 comments

Flutter Version Flutter stable: 3.22.1

Lib Version assets_audio_player: ^3.1.1

** Platform (Android / iOS ) **

Two audio tracks play simultaneously when I close and reopen the app. I tried to call the stop() method, but it didn't work to stop the first track.

Future<List<StationModel>> onInitApp() async {

    bool isPlaying = audioPlayer.isPlaying.value;
    if (isPlaying) await audioPlayer.stop();

    final repository = ref.watch(radioRepositoryProvider);
    final result = await repository.getHistorys(stations);
    return result.fold((l) => throw l.message, (r) async {
      Box<dynamic> settings = Boxes.settings();
      int lastIndex = settings.get(Keys.lastIndex, defaultValue: 0);
      List<Audio> audios = _stationToMediaItem(r);
      await audioPlayer.open(
        Playlist(
          audios: audios,
          startIndex: lastIndex,
        ),
        showNotification: true,
        autoStart: true,
      );
      return r;
    });
  }

MazenCoder avatar Jun 16 '24 14:06 MazenCoder

@MazenCoder I worked around the issue using a boolean:

if(inicialInit){ inicialInit = false

assetsAudioPlayer = AssetsAudioPlayer();

  await assetsAudioPlayer.open(
    playInBackground: PlayInBackground.enabled,
    autoStart: true,
    showNotification: true,
    Audio.liveStream(streamUrl),
  );

}

Because I was having some issues with didChangeDependencies and the method "bool isPlaying = audioPlayer.isPlaying.value;""

So I only inicialize once and any other call to the assetAudioPlayer is only using .play() and .pause().

Also, for some reason, on ios the issue still persists when I make changes to code and using the hot reload and hot restart. But when using the app and not debugging with changes it works fine.

raj-arthur-lima avatar Apr 26 '25 19:04 raj-arthur-lima