Do I need to call dispose when I use FlameAudio.play('xxx.mp3')?

477

Depends on the ReleaseMode of the player. If set to ReleaseMode.DISPOSE, it will automatically dispose when finished playing.

If set to STOP, you are manually controlling it. So you must call dispose when no longer used. If you have a single instance of audioplayers that you share on the entire app, then you can keep using it. But if you are creating multiple, it's paramount to dispose them when you no longer need each instance (on STOP mode).

To know if a player has finished, you can listen to the stream onPlayerStateChanged.

Share:
477
i hate microsoft
Author by

i hate microsoft

Updated on January 02, 2023

Comments

  • i hate microsoft
    i hate microsoft over 1 year
    1. FlameAudio.play will create an AudioPlayer
    2. The AudioPlayer documentation requires that the dispose function be called when it is no longer used
        /// Closes all [StreamController]s.
        ///
        /// You must call this method when your [AudioPlayer] instance is not going to
        /// be used anymore. If you try to use it after this you will get errors.
        Future<void> dispose() async 
      
    3. But I didn’t see Flame stating whether it is necessary to call the dispose function, and I don’t know how to be get notified of the end of the playback.

    So do I need to call dispose? If that is necessary how to know that the audio has finished playing

  • i hate microsoft
    i hate microsoft over 2 years
    Thanks for your answer. But when the AudioPlayer instance was created, a key was added to the Map. Will ReleaseMode automatically delete this key? I did not find the code for automatic deletion. If it will not be deleted automatically, how to prevent the memory leak caused by the key? ``` static final players = <String, AudioPlayer>{}; AudioPlayer({this.mode = PlayerMode.MEDIA_PLAYER, String? playerId}) : playerId = playerId ?? _uuid.v4() { players[this.playerId] = this; notificationService = NotificationService(_invokeMethod); } ```
  • i hate microsoft
    i hate microsoft over 2 years
    For PlayerMode.LOW_LATENCY mode, onPlayerStateChanged won’t work, so how can I release resources(call dispose()) at an appropriate time? I use audioplayers 0.20.1 and flame_audio 1.0.0 on android
  • Luan Nico
    Luan Nico over 2 years
    It should be removed by the dispose method For: > For PlayerMode.LOW_LATENCY mode, onPlayerStateChanged I did not know that, but it does appear to be the case. That sounds like an important feature to tackle, I will try to take a look!