audioPlaybackControl method

Future<bool> audioPlaybackControl({
  1. required double startValue,
  2. required double endValue,
})

For getting the audio controller state, to know whether the audio is playing or paused currently.

The two required parameters are startValue & endValue

  • startValue is the current starting point of the audio.
  • endValue is the current ending point of the audio.

Returns a Future<bool>, if true then audio is playing otherwise paused.

Implementation

Future<bool> audioPlaybackControl({
  required double startValue,
  required double endValue,
}) async {
  if (audioPlayer?.state == PlayerState.playing) {
    await audioPlayer?.pause();
    return false;
  } else {
    final Duration? duration = await audioPlayer!.getCurrentPosition();
    if ((duration?.inMilliseconds ?? 0) >= endValue.toInt()) {
      await audioPlayer!.seek(Duration(milliseconds: startValue.toInt()));
      await audioPlayer!.resume();
      return true;
    } else {
      await audioPlayer!.resume();
      return true;
    }
  }
}