fadeTo method
Fade To Target Volume
Tween volume from current level to targetVolume over duration
following curve. Uses a ~60 FPS stepping loop for smoothness.
Edge Cases:
- If
durationis very small, we still perform at least one step.
Implementation
Future<void> fadeTo(
double targetVolume, {
Duration duration = const Duration(seconds: 2),
Curve curve = Curves.linear,
}) async {
final double start = _volume;
final double end = targetVolume;
const int fps = 60;
int steps = (fps * duration.inMilliseconds / 1000).round();
if (steps < 1) steps = 1; // ensure at least one step
final frameDuration = Duration(milliseconds: (1000 / fps).round());
FiftyAudioLogger.fade(start, end, duration, curve);
for (int i = 0; i <= steps; i++) {
final t = curve.transform(i / steps);
final v = start + (end - start) * t;
await _player.setVolume(v);
_muted = v == 0.0;
await Future.delayed(frameDuration);
}
_volume = targetVolume;
}