preparePlayer method

Future<void> preparePlayer({
  1. required String path,
  2. double? volume,
  3. bool shouldExtractWaveform = true,
  4. int noOfSamples = 100,
})

Calls platform to prepare player.

Path is required parameter for providing location of the audio file.

volume is optional parameters with minimum value 0.0 is treated as mute and 1.0 as max volume. Providing value greater 1.0 is also treated same as 1.0 (max volume).

Waveforms also will be extracted when with function which can be accessed using waveformData. Passing false to shouldExtractWaveform will prevent extracting of waveforms.

Waveforms also can be extracted using extractWaveformData function which can be stored locally or over the server. This data can be passed directly passed to AudioFileWaveforms widget. This will save the resources when extracting waveforms for same file everytime.

noOfSamples indicates no of extracted data points. This will determine number of bars in the waveform.

Defaults to 100.

Implementation

Future<void> preparePlayer({
  required String path,
  double? volume,
  bool shouldExtractWaveform = true,
  int noOfSamples = 100,
}) async {
  path = Uri.parse(path).path;
  final isPrepared = await AudioWaveformsInterface.instance.preparePlayer(
    path: path,
    key: playerKey,
    frequency: updateFrequency.value,
    volume: volume,
    overrideAudioSession: overrideAudioSession,
  );
  if (isPrepared) {
    _maxDuration = await getDuration();
    _setPlayerState(PlayerState.initialized);
  }

  if (shouldExtractWaveform) {
    extractWaveformData(
      path: path,
      noOfSamples: noOfSamples,
    ).then(
      (value) {
        waveformData
          ..clear()
          ..addAll(value);
        notifyListeners();
      },
    );
  }
  notifyListeners();
}