A cross platform Audiobookshelf client for Android and iOS
Dart
Stop a stuck player counting hours you never listened to and moving your place in a book backwards
Barnabasff56d3f105ff
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dartindex 57ab9884..1d058c90 100644--- a/lib/services/audio_player_service.dart+++ b/lib/services/audio_player_service.dart@@ -3368,6 +3368,7 @@ class AudioPlayerService extends ChangeNotifier {_activeStreamHeaders = const {};_lastServerSync = DateTime.now();_lastAccrual = _lastServerSync;+ _lastAccrualPos = null;_logEvent(PlaybackEventType.sessionStart, detail: 'local hot-swap');if (api == null || streamingSessionId == null) return;@@ -4891,6 +4892,7 @@ class AudioPlayerService extends ChangeNotifier {_lastKnownPositionSec = 0;_lastServerSync = DateTime.now();_lastAccrual = DateTime.now();+ _lastAccrualPos = null;_positionSyncInProgress = false;_positionSyncFailures = 0;// Cache prefs in background - not needed synchronously here@@ -5591,6 +5593,7 @@ class AudioPlayerService extends ChangeNotifier {// Separate from the server-sync clock: drives the every-5s durable accrual so// a kill loses at most one tick of listening, independent of how often we POST.DateTime _lastAccrual = DateTime.now();+ double? _lastAccrualPos;bool _syncRecoveryInProgress = false;bool _positionSyncInProgress = false;int _positionSyncFailures = 0;@@ -5611,13 +5614,29 @@ class AudioPlayerService extends ChangeNotifier {final now = DateTime.now();final delta = now.difference(_lastAccrual).inSeconds;if (delta <= 0) return;+ final ct = absolutePos.inMilliseconds / 1000.0;+ // A player can report playing while its position sits still - a stalled+ // stream, a dead web audio element. Wall clock alone would bank that span+ // as listening, and the 300s backstop timer keeps doing it for as long as+ // the app is open, so hours of standing still later ship as one phantom+ // session. Only credit a span the position actually moved through, and+ // drop the frozen span rather than carrying it into the next tick.+ final prevPos = _lastAccrualPos;+ _lastAccrualPos = ct;+ if (prevPos != null && (ct - prevPos).abs() < 0.5) {+ _lastAccrual = now;+ debugPrint(+ '[Player] Position frozen at ${ct.toStringAsFixed(1)}s - '+ 'not banking ${delta}s as listening',+ );+ return;+ }final secs = delta > 300 ? 300 : delta;_lastAccrual = now;final key = _currentEpisodeId != null? '$_currentItemId-$_currentEpisodeId': _currentItemId!;final manualOffline = (_prefs?.getBool('manual_offline_mode')) ?? false;- final ct = absolutePos.inMilliseconds / 1000.0;if (_localSessionMode) {await LocalSessionService().accrue(progressKey: key,@@ -5884,6 +5903,7 @@ class AudioPlayerService extends ChangeNotifier {// include pause duration as timeListened_lastServerSync = DateTime.now();_lastAccrual = DateTime.now();+ _lastAccrualPos = null;// Re-activate audio session in case a prior stop released it.try {(await AudioSession.instance).setActive(true);@@ -5931,6 +5951,7 @@ class AudioPlayerService extends ChangeNotifier {// would otherwise be credited as up to 300s of phantom listening)._lastServerSync = DateTime.now();_lastAccrual = DateTime.now();+ _lastAccrualPos = null;// Start playback immediately — don't wait for server calls_player?.play();_scheduleAudioDiagnostics('resume');diff --git a/lib/services/progress_sync_service.dart b/lib/services/progress_sync_service.dartindex e683305e..ec58ad87 100644--- a/lib/services/progress_sync_service.dart+++ b/lib/services/progress_sync_service.dart@@ -564,9 +564,38 @@ class ProgressSyncService {}final data = await getLocal(itemId);- final currentTime = (data?['currentTime'] as num?)?.toDouble() ?? 0;+ var currentTime = (data?['currentTime'] as num?)?.toDouble() ?? 0;final duration = (data?['duration'] as num?)?.toDouble() ?? 0;+ // The session opened below carries a position, and the server writes+ // that position into progress. This ledger entry can be hours older+ // than the server's own position (another device, or this one, carried+ // on listening since), so report the time at whichever position is+ // further in. Crediting listening must never drag progress backwards.+ try {+ final serverProgress = await api.getItemProgress(itemId);+ final serverTime =+ (serverProgress?['currentTime'] as num?)?.toDouble() ?? 0;+ if (serverTime > currentTime) {+ debugPrint(+ '[Sync] Server is ahead for $itemId (${serverTime}s vs ${currentTime}s) - reporting listening at the server position');+ currentTime = serverTime;+ }+ } catch (e) {+ debugPrint('[Sync] Could not read server progress for $itemId: $e');+ }++ // A ledger holding more listening than the item is long is not a real+ // backlog, it is an accrual that ran away - a player that reported+ // playing while its position stood still. Report what is plausible and+ // say what was dropped rather than writing fiction into the stats.+ var reported = seconds;+ if (duration > 0 && reported > duration) {+ debugPrint(+ '[Sync] Offline listening for $itemId holds ${seconds}s but the item is only ${duration.round()}s - reporting ${duration.round()}s and dropping the rest');+ reported = duration.round();+ }+try {final isCompound = itemId.length > 36;final apiItemId = isCompound ? itemId.substring(0, 36) : itemId;@@ -583,10 +612,10 @@ class ProgressSyncService {sid,currentTime: currentTime,duration: duration,- timeListened: seconds,+ timeListened: reported,);await api.closePlaybackSession(sid);- debugPrint('[Sync] Flushed ${seconds}s offline listening for $itemId');+ debugPrint('[Sync] Flushed ${reported}s offline listening for $itemId');}}// Subtract only what we actually synced. Any ticks added to the