Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Show sleep timer rewinds as auto-rewinds in playback history

Barnabas09298e4a7acd
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dart
index 568f582c..fa937f52 100644
--- a/lib/services/audio_player_service.dart
+++ b/lib/services/audio_player_service.dart
@@ -4315,17 +4315,30 @@ class AudioPlayerService extends ChangeNotifier {
}
}
- Future<void> seekTo(Duration pos) async {
+ Future<void> seekTo(Duration pos,
+ {PlaybackEventType logAs = PlaybackEventType.seek, String? logDetail}) async {
_resetStuckDetection();
if (_player != null && !_player!.playing) _seekedWhilePaused = true;
final from = position;
await _seekAbsolute(pos.inMilliseconds / 1000.0);
- _logEvent(PlaybackEventType.seek,
- detail: '${_formatPos(from)} → ${_formatPos(pos)}',
+ _logEvent(logAs,
+ detail: logDetail ?? '${_formatPos(from)} → ${_formatPos(pos)}',
overridePosition: from.inMilliseconds / 1000.0);
notifyListeners();
}
+ /// Rewind triggered by the sleep timer firing. Same mechanics as a manual
+ /// seek-back, but logged as an auto-rewind so the playback history reads
+ /// "Auto-rewound 15m (sleep timer)" instead of a raw seek (GH #232).
+ Future<void> sleepTimerRewind(int seconds) async {
+ if (seconds <= 0) return;
+ var target = position - Duration(seconds: seconds);
+ if (target < Duration.zero) target = Duration.zero;
+ final amount = seconds % 60 == 0 ? '${seconds ~/ 60}m' : '${seconds}s';
+ await seekTo(target,
+ logAs: PlaybackEventType.autoRewind, logDetail: '$amount (sleep timer)');
+ }
+
Future<void> skipForward([int seconds = 30]) async {
if (_player == null) return;
_resetStuckDetection();
diff --git a/lib/services/sleep_timer_service.dart b/lib/services/sleep_timer_service.dart
index 4fd33028..0cc5a164 100644
--- a/lib/services/sleep_timer_service.dart
+++ b/lib/services/sleep_timer_service.dart
@@ -360,9 +360,7 @@ class SleepTimerService extends ChangeNotifier {
// Auto-rewind so the user resumes from a few seconds back
final rewindSeconds = await PlayerSettings.getEffectiveSleepRewindSeconds(_player.currentItemId);
if (rewindSeconds > 0) {
- final currentPos = _player.position;
- final newPos = currentPos - Duration(seconds: rewindSeconds);
- _player.seekTo(newPos < Duration.zero ? Duration.zero : newPos);
+ await _player.sleepTimerRewind(rewindSeconds);
debugPrint('[SleepTimer] Rewound ${rewindSeconds}s');
}
}