Files
plezy/lib/utils/player_utils.dart
T

24 lines
723 B
Dart
Raw Normal View History

2025-11-27 07:32:07 +01:00
import '../mpv/mpv.dart';
2025-11-22 06:33:49 +01:00
/// Seeks by the given offset (can be positive or negative) while clamping
2025-12-18 08:42:18 +01:00
/// the result between 0 and the video duration.
/// Returns the clamped position that was seeked to.
Duration seekWithClamping(Player player, Duration offset) {
2025-11-22 06:33:49 +01:00
final currentPosition = player.state.position;
final duration = player.state.duration;
final newPosition = currentPosition + offset;
// Clamp between 0 and video duration
2026-02-15 03:14:04 +01:00
Duration clampedPosition;
if (newPosition.isNegative) {
clampedPosition = Duration.zero;
} else if (newPosition > duration) {
clampedPosition = duration;
} else {
clampedPosition = newPosition;
}
2025-11-22 06:33:49 +01:00
player.seek(clampedPosition);
2025-12-18 08:42:18 +01:00
return clampedPosition;
2025-11-22 06:33:49 +01:00
}