preparePlayer method

Future<void> preparePlayer(
  1. String path, [
  2. double? volume
])

Call this to prepare player with optional volume parameters (has to be between 0.0 to 1.0).

It first reads bytes from audio file so as soon as it completes it prepares audio player.

playerState has to be PlayerState.readingComplete when preparing player otherwise throws Exception.

This behavior is set to ensure that player is only re-initialised for new audio file.

Implementation

Future<void> preparePlayer(String path, [double? volume]) async {
  await _readAudioFile(path);
  if ((_playerState == PlayerState.readingComplete &&
      _audioFilePath != null)) {
    final isPrepared = await AudioWaveformsInterface.instance
        .preparePlayer(path, _playerKey.toString(), volume);
    if (isPrepared) {
      _maxDuration = await getDuration();
      setPlayerState(PlayerState.initialized);
    }
    notifyListeners();
  } else {
    throw "Can not call without reading new audio file";
  }
}