dispose method

Future<void> dispose()

Closes all StreamControllers.

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.

Implementation

Future<void> dispose() async {
  // First stop and release all native resources.
  await release();

  state = desiredState = PlayerState.disposed;

  final futures = <Future>[
    if (_positionUpdater != null) _positionUpdater!.dispose(),
    if (!_playerStateController.isClosed) _playerStateController.close(),
    _onPlayerCompleteStreamSubscription.cancel(),
    _onLogStreamSubscription.cancel(),
    _eventStreamSubscription.cancel(),
    _eventStreamController.close(),
  ];

  _source = null;

  await Future.wait<dynamic>(futures);

  // Needs to be called after cancelling event stream subscription:
  await _platform.dispose(playerId);
}