lerpBackroundVolume method

Future<void> lerpBackroundVolume(
  1. double to,
  2. Duration time
)

Lineary interpolates the volume of background to a provided value.

Implementation

Future<void> lerpBackroundVolume(double to, Duration time) {
  if (_backgroundPlayer == null) return Future.value();
  final result = Completer();

  final delta = to - _backgroundPlayer!.volume;
  final start = _backgroundPlayer!.volume;
  //assuming one audio tick as 1/20 of a second
  final ticks = time.inSeconds * 20;

  Timer.periodic(const Duration(milliseconds: 50), (timer) {
    if (timer.tick >= ticks) {
      result.complete();
      timer.cancel();
    }
    final v = start + delta * timer.tick / ticks;
    _backgroundPlayer?.setVolume(v);
  });

  return result.future;
}