dispose method

Future<void> dispose()

Dispose this Audio.

This must be called before object falls out of its local scope, or else a memory leak may occur. Once dispose is called, no further calls to the Audio object are accepted. Triggers release of the underlying audio resources, either immediately, or on playback completion. Note that on calling dispose, audio playback will continue, and onComplete will still be called on playback completion.

Implementation

Future<void> dispose() async {
  if (!_undisposedAudios.containsKey(_audioId)) {
    _logger.severe('Called dispose() on a disposed Audio');
    return;
  }
  _undisposedAudios.remove(_audioId);

  // If not playing, call for release immediately. Otherwise (if audio is
  // playing) it will be called when playback completes.
  if (!_playing) {
    _usingOnErrorAudios.remove(_audioId);
    WidgetsBinding.instance!.removeObserver(this);
    await _releaseNative(_audioId);
  }
}