A cross platform Audiobookshelf client for Android and iOS
Dart
Start playing right away from headphones, the widget and Android Auto instead of waiting on the server, and only check for progress from another device when you press play in the app
Barnabas43477c5f4c8e
diff --git a/lib/screens/absorbing_screen.dart b/lib/screens/absorbing_screen.dartindex 255aef72..4c4e1389 100644--- a/lib/screens/absorbing_screen.dart+++ b/lib/screens/absorbing_screen.dart@@ -1946,6 +1946,7 @@ class _ReorderAbsorbingSheetState extends State<_ReorderAbsorbingSheet> {episodeId: epId,episodeTitle: epTitle,libraryId: book['libraryId'] as String? ?? widget.lib.selectedLibraryId,+ fromUi: true,);} else {playback = AudioPlayerService().playItem(@@ -1957,6 +1958,7 @@ class _ReorderAbsorbingSheetState extends State<_ReorderAbsorbingSheet> {totalDuration: (media['duration'] as num?)?.toDouble() ?? 0,chapters: media['chapters'] as List<dynamic>? ?? const [],libraryId: book['libraryId'] as String? ?? widget.lib.selectedLibraryId,+ fromUi: true,);}unawaited(playback.then((error) async {diff --git a/lib/screens/app_shell.dart b/lib/screens/app_shell.dartindex 1884bcda..df9e1de5 100644--- a/lib/screens/app_shell.dart+++ b/lib/screens/app_shell.dart@@ -865,7 +865,7 @@ class _AppShellState extends State<AppShell>if (_player.isPlaying) {_player.pause();} else {- _player.play();+ _player.play(fromUi: true);}}}diff --git a/lib/screens/car_mode_screen.dart b/lib/screens/car_mode_screen.dartindex 8bf8b31e..42220d3a 100644--- a/lib/screens/car_mode_screen.dart+++ b/lib/screens/car_mode_screen.dart@@ -139,6 +139,7 @@ class _CarModeScreenState extends State<CarModeScreen>episodeId: widget.episodeId,episodeTitle: widget.episodeTitle,libraryId: context.read<LibraryProvider>().selectedLibraryId,+ fromUi: true,);if (mounted) setState(() => _isStarting = false);}diff --git a/lib/screens/home_screen.dart b/lib/screens/home_screen.dartindex 0e03c793..9b1f8e71 100644--- a/lib/screens/home_screen.dart+++ b/lib/screens/home_screen.dart@@ -995,7 +995,7 @@ class _ContinueListeningCardState extends State<_ContinueListeningCard> {if (serverCurrentTime > playerPosSec + 5.0) {_startBook(context, itemId);} else {- player.play();+ player.play(fromUi: true);}}} else {@@ -1245,6 +1245,7 @@ class _ContinueListeningCardState extends State<_ContinueListeningCard> {episodeId: episodeId,episodeTitle: episodeTitle,libraryId: widget.item['libraryId'] as String?,+ fromUi: true,);if (mounted) {if (error != null) showErrorToast(context, error);@@ -1280,6 +1281,7 @@ class _ContinueListeningCardState extends State<_ContinueListeningCard> {totalDuration: duration,chapters: chapters,libraryId: fullItem['libraryId'] as String?,+ fromUi: true,);if (error != null && mounted) showErrorToast(context, error);diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dartindex 2821633b..2c6805aa 100644--- a/lib/services/audio_player_service.dart+++ b/lib/services/audio_player_service.dart@@ -2986,8 +2986,14 @@ class AudioPlayerService extends ChangeNotifier {String? episodeId,String? episodeTitle,String? libraryId,+ // Started from the app's own screen. Only those wait (briefly, capped)+ // for the server's saved position before audio; plays from Android+ // Auto, the widget, headphones or a cold-start restore begin at the+ // phone's own position immediately. See play().+ bool fromUi = false,}) async {_pauseRequested = false;+ _playFromUi = fromUi;if (_handler == null) {debugPrint('[Player] Handler not yet initialized, waiting…');await _initCompleter.future;@@ -3002,7 +3008,7 @@ class AudioPlayerService extends ChangeNotifier {// Handler.play() / Service.play(). Strip before next beta.debugPrint('[PlayItemEntry] itemId=$itemId episodeId=$episodeId '- 'startTime=$startTime forceStartTime=$forceStartTime\n'+ 'startTime=$startTime forceStartTime=$forceStartTime fromUi=$fromUi\n''Caller:\n${StackTrace.current}',);@@ -3165,15 +3171,20 @@ class AudioPlayerService extends ChangeNotifier {// Race the real session against the cache. When the server answers// inside the window (LAN, good WiFi) the play starts through the// fresh path with tokenless session URLs - immune to token expiry -- // and only a slow server falls back to the instant cached start.+ // and its saved position; only a slow server falls back to the+ // instant cached start. An app-screen play waits the full position+ // cap since that is its one chance to pick up another device's+ // progress before audio; everything else keeps the short window.// The in-flight POST is handed to the background refresh either way// so a second session is never opened.final pendingSession = episodeId != null? api.startEpisodePlaybackSession(itemId, episodeId): api.startPlaybackSession(itemId);+ final raceWindow =+ fromUi ? _serverPositionCheckCap : _sessionRaceWindow;Map<String, dynamic>? racedSession;try {- racedSession = await pendingSession.timeout(_sessionRaceWindow);+ racedSession = await pendingSession.timeout(raceWindow);} catch (_) {racedSession = null;}@@ -3251,46 +3262,6 @@ class AudioPlayerService extends ChangeNotifier {if (result != null) _endAdvanceBuffering();notifyListeners();- // Session-start rewind: rewind by maxRewind when starting a new session- if (result == null && !forceStartTime) {- final rewindSettings = await AutoRewindSettings.load();- if (rewindSettings.enabled && rewindSettings.sessionStartRewind) {- final rewindSeconds = rewindSettings.maxRewind;- if (rewindSeconds > 0.5 && _player != null) {- final currentAbsolutePos = position.inMilliseconds / 1000.0;- // Scale by speed so the listener gets the same perceived amount of- // re-orientation time regardless of playback speed - at 1.5x a 2s- // setting covers 3s of book content (= 2s of real listening time).- final currentSpeed = _player!.speed;- var newPosSeconds =- currentAbsolutePos - (rewindSeconds * currentSpeed);- if (newPosSeconds < 0) newPosSeconds = 0;- if (rewindSettings.chapterBarrier && _chapters.isNotEmpty) {- for (final ch in _chapters) {- final start = (ch['start'] as num?)?.toDouble() ?? 0;- final end = (ch['end'] as num?)?.toDouble() ?? 0;- if (currentAbsolutePos >= start && currentAbsolutePos < end) {- if (newPosSeconds < start) newPosSeconds = start;- break;- }- }- }- await _seekAbsolute(newPosSeconds);- // Use actual book-time delta so chapter barrier caps are reflected.- final actualDelta = currentAbsolutePos - newPosSeconds;- _lastAutoRewindAmount = actualDelta;- final detail = currentSpeed == 1.0- ? '${rewindSeconds.toStringAsFixed(1)}s (session start)'- : '${rewindSeconds.toStringAsFixed(1)}s (${actualDelta.toStringAsFixed(1)}s at ${currentSpeed.toStringAsFixed(2)}x, session start)';- _logEvent(PlaybackEventType.autoRewind, detail: detail);- debugPrint(- '[Player] Session-start rewind ${rewindSeconds.toStringAsFixed(1)}s '- '(${actualDelta.toStringAsFixed(1)}s at ${currentSpeed.toStringAsFixed(2)}x)',- );- }- }- }-if (result == null && playbackGeneration == _playbackGeneration) {_onPlaybackStateChangedCallback?.call(true);// Auto-navigate to Absorbing tab when an episode starts playing.@@ -3299,6 +3270,50 @@ class AudioPlayerService extends ChangeNotifier {return result;}+ /// Session-start rewind (setting): pull a new session's start position back+ /// by maxRewind so playback opens with a little re-orientation. Scaled by+ /// speed so the perceived amount is the same at 1.5x, and capped at the+ /// chapter start when the barrier is on. Applied to the start position+ /// BEFORE the first seek - it used to run after play() and audibly jumped+ /// 50-90ms into live audio. Callers pass the speed they're about to set.+ Future<double> _sessionStartRewound(+ double startTime, {+ required bool forceStartTime,+ required double speed,+ }) async {+ if (forceStartTime || startTime <= 0) return startTime;+ final rewindSettings = await AutoRewindSettings.load();+ if (!rewindSettings.enabled || !rewindSettings.sessionStartRewind) {+ return startTime;+ }+ final rewindSeconds = rewindSettings.maxRewind;+ if (rewindSeconds <= 0.5) return startTime;+ var newPos = startTime - (rewindSeconds * speed);+ if (newPos < 0) newPos = 0;+ if (rewindSettings.chapterBarrier && _chapters.isNotEmpty) {+ for (final ch in _chapters) {+ final start = (ch['start'] as num?)?.toDouble() ?? 0;+ final end = (ch['end'] as num?)?.toDouble() ?? 0;+ if (startTime >= start && startTime < end) {+ if (newPos < start) newPos = start;+ break;+ }+ }+ }+ final actualDelta = startTime - newPos;+ if (actualDelta <= 0) return startTime;+ final detail = speed == 1.0+ ? '${rewindSeconds.toStringAsFixed(1)}s (session start)'+ : '${rewindSeconds.toStringAsFixed(1)}s (${actualDelta.toStringAsFixed(1)}s at ${speed.toStringAsFixed(2)}x, session start)';+ _logEvent(PlaybackEventType.autoRewind, detail: detail);+ debugPrint(+ '[Player] Session-start rewind ${rewindSeconds.toStringAsFixed(1)}s '+ '(${actualDelta.toStringAsFixed(1)}s at ${speed.toStringAsFixed(2)}x) '+ '-> starting at ${newPos.toStringAsFixed(1)}s',+ );+ return newPos;+ }+/// Hot-swap from streaming to local files without interrupting playback position./// Called when a download completes for the currently-playing item.Future<bool> switchToLocal(String itemId) async {@@ -3499,11 +3514,22 @@ class AudioPlayerService extends ChangeNotifier {final pKey = _currentEpisodeId != null? '$_currentItemId-$_currentEpisodeId': _currentItemId!;- if (_api != null && !manualOffline && !_knownOffline) {+ // Only a play from the app screen waits on this, and only briefly: it is+ // the one moment a fresh start can pick up another device's progress+ // before audio. Headphone / widget / Android Auto / cold-start plays of+ // a download need nothing from the network to begin, so they don't ask+ // (GH #321) - unless this phone has no position for it at all, where+ // starting from zero would be the wrong guess and there is nothing to+ // preserve. Whatever position we start with, we keep - no seeking after.+ if (!_playFromUi && startTime > 0) {+ debugPrint(+ '[Player] Skipping progress reconcile - not started from the app screen, playing local position ${startTime.toStringAsFixed(1)}s now',+ );+ } else if (_api != null && !manualOffline && !_knownOffline) {try {final serverProgress = await _api!.getItemProgress(pKey)- .timeout(const Duration(seconds: 5), onTimeout: () => null);+ .timeout(_serverPositionCheckCap, onTimeout: () => null);final serverPos =(serverProgress?['currentTime'] as num?)?.toDouble() ?? 0;final serverLastUpdate =@@ -3659,6 +3685,14 @@ class AudioPlayerService extends ChangeNotifier {// If the saved position is at (or past) the end, restart from the beginningif (totalDuration > 0 && startTime >= totalDuration - 1.0) startTime = 0;+ final speedKey = _currentItemId ?? itemId;+ final bookSpeed = await PlayerSettings.getBookSpeed(speedKey);+ final speed = bookSpeed ?? await PlayerSettings.getDefaultSpeed();+ startTime = await _sessionStartRewound(+ startTime,+ forceStartTime: forceStartTime,+ speed: speed,+ );if (startTime > 0) {await _seekAbsolute(startTime);}@@ -3670,9 +3704,6 @@ class AudioPlayerService extends ChangeNotifier {// speed-adjusted at push time, and nothing re-pushes it on load - so// pushing at the default 1.0x left the notification/AA/widget bar// lagging (the chapter "ended" at 1/speed) until a foreground re-push.- final speedKey = _currentItemId ?? itemId;- final bookSpeed = await PlayerSettings.getBookSpeed(speedKey);- final speed = bookSpeed ?? await PlayerSettings.getDefaultSpeed();await _player!.setSpeed(speed);_pushMediaItem(itemId,@@ -3844,6 +3875,13 @@ class AudioPlayerService extends ChangeNotifier {_currentBookTrackCount = trackSources.length;if (totalDuration > 0 && startTime >= totalDuration - 1.0) startTime = 0;+ final bookSpeed = await PlayerSettings.getBookSpeed(itemId);+ final speed = bookSpeed ?? await PlayerSettings.getDefaultSpeed();+ startTime = await _sessionStartRewound(+ startTime,+ forceStartTime: forceStartTime,+ speed: speed,+ );if (startTime > 0) {await _seekAbsolute(startTime);}@@ -3852,8 +3890,6 @@ class AudioPlayerService extends ChangeNotifier {_subscribeTrackIndex();final initChapter = _initChapterInfo(startTime);// Speed before _pushMediaItem - the pushed duration is speed-adjusted.- final bookSpeed = await PlayerSettings.getBookSpeed(itemId);- final speed = bookSpeed ?? await PlayerSettings.getDefaultSpeed();await _player!.setSpeed(speed);_pushMediaItem(itemId,@@ -3973,9 +4009,10 @@ class AudioPlayerService extends ChangeNotifier {_stashSessionUpgrade(sessionData);}- // Check if server position is ahead (another client advanced). Gate on a- // newer server timestamp, not position alone, so a stale pre-rewind- // position left on the server can't yank us forward (see _resumeServerSync).+ // Audio is already running from the cached start; the server's saved+ // position is only logged here, never seeked to - a jump a few seconds+ // into playback is worse than the stale spot. An app-screen play had+ // its chance to adopt it in the pre-start race window.final serverPos =(serverProgress?['currentTime'] as num?)?.toDouble() ??(sessionData['currentTime'] as num?)?.toDouble() ??@@ -3994,14 +4031,7 @@ class AudioPlayerService extends ChangeNotifier {currentPlaybackTime: localPos,)) {debugPrint(- '[Player] Server is ahead on cache-start: server=${serverPos}s vs local=${localPos}s - seeking',- );- await _seekAbsolute(serverPos);- await _progressSync.saveLocal(- itemId: progressKey,- currentTime: serverPos,- duration: _totalDuration,- speed: speed,+ '[Player] Server is ahead on cache-start: server=${serverPos}s vs local=${localPos}s - not seeking (audio already running)',);}} catch (e) {@@ -4266,6 +4296,13 @@ class AudioPlayerService extends ChangeNotifier {// If the saved position is at (or past) the end, restart from the beginningif (totalDuration > 0 && startTime >= totalDuration - 1.0) startTime = 0;+ final bookSpeed = await PlayerSettings.getBookSpeed(itemId);+ final speed = bookSpeed ?? await PlayerSettings.getDefaultSpeed();+ startTime = await _sessionStartRewound(+ startTime,+ forceStartTime: forceStartTime,+ speed: speed,+ );if (startTime > 0) {await _seekAbsolute(startTime);}@@ -4274,8 +4311,6 @@ class AudioPlayerService extends ChangeNotifier {_subscribeTrackIndex();final initChapter = _initChapterInfo(startTime);// Speed before _pushMediaItem - the pushed duration is speed-adjusted.- final bookSpeed = await PlayerSettings.getBookSpeed(itemId);- final speed = bookSpeed ?? await PlayerSettings.getDefaultSpeed();await _player!.setSpeed(speed);_pushMediaItem(itemId,@@ -5845,8 +5880,10 @@ class AudioPlayerService extends ChangeNotifier {}DateTime? _lastPauseTime;- double _lastAutoRewindAmount = 0;bool _seekedWhilePaused = false;+ // Origin of the playItem in flight, read by the play paths to decide+ // whether to wait on the server's saved position. See playItem().+ bool _playFromUi = false;bool _wasPlayingBeforeInterrupt = false;bool _pauseRequested = false;bool get isPauseRequested => _pauseRequested;@@ -5889,9 +5926,17 @@ class AudioPlayerService extends ChangeNotifier {return rewind.clamp(minRewind, maxRewind);}- Future<void> play({String? logDetail}) async {+ /// [fromUi] marks a play started from the app's own screen. Only those+ /// look at the server for a position another device may have advanced,+ /// and only before audio starts. Headphones, notification, widget, lock+ /// screen and Android Auto plays resume where the phone left off, right+ /// away - if you switched devices you'll open the app, and a jump a few+ /// seconds into playback is worse than starting where you paused.+ Future<void> play({String? logDetail, bool fromUi = false}) async {_pauseRequested = false;- debugPrint('[Service] play() called — lastPause=${_lastPauseTime != null}');+ debugPrint(+ '[Service] play() called — lastPause=${_lastPauseTime != null} fromUi=$fromUi',+ );// Cold-start play guard. If the OS killed absorb during a long pause// and the user tapped play via headphones / lock screen / Android Auto,@@ -5931,15 +5976,27 @@ class AudioPlayerService extends ChangeNotifier {_noisyPause =false; // User explicitly resumed — allow interrupt-resume again_handler?._noisyPauseAt = null; // Clear noisy suppression window- _lastAutoRewindAmount = 0;+ // A seek while paused (user, or the socket adopting another device's+ // position) is the position the user expects to hear next - don't let+ // the server check below override it.+ final seekedWhilePaused = _seekedWhilePaused;+ _seekedWhilePaused = false;+ final adoptedServerPos = fromUi && !seekedWhilePaused+ ? await _adoptServerPositionBeforeResume()+ : false;// Coming back within a few minutes of the sleep timer firing means you were// awake for it, so put back what its rewind took. Done before the ordinary// auto-rewind below so that still applies from the restored position.+ // Moot when we just jumped to another device's position; consume the undo+ // anyway so it can't fire on a later resume.if (_player != null) {final undoTo = SleepTimerService().takeSleepRewindUndo(_currentItemId, position);- if (undoTo != null) {+ if (undoTo != null && !adoptedServerPos) {await seekTo(undoTo, logDetail: 'sleep rewind undone');+ // seekTo flags a paused seek; this one is ours, not the user's, and+ // the server check has already run, so don't carry it to next resume.+ _seekedWhilePaused = false;}}// Auto-rewind on resume if enabled@@ -5971,14 +6028,9 @@ class AudioPlayerService extends ChangeNotifier {}}await _seekAbsolute(newPosSeconds);- // Store the actual book-time position delta, not raw rewindSeconds.- // At speed>1.0 the delta is larger (rewindSeconds * speed), and the- // chapter barrier above may cap it smaller. The "server ahead on- // resume" check compares serverPos vs localPos+_lastAutoRewindAmount;- // using raw seconds at 1.4x makes it misread a legitimate auto-rewind- // gap as the server being ahead and seeks forward, erasing the rewind.+ // Log the actual book-time delta: at speed>1.0 it's larger than+ // rewindSeconds, and the chapter barrier may cap it smaller.final actualDelta = currentAbsolutePos - newPosSeconds;- _lastAutoRewindAmount = actualDelta;final rewindDetail = currentSpeed == 1.0? '${rewindSeconds.toStringAsFixed(1)}s': '${rewindSeconds.toStringAsFixed(1)}s (${actualDelta.toStringAsFixed(1)}s at ${currentSpeed.toStringAsFixed(2)}x)';@@ -6035,6 +6087,7 @@ class AudioPlayerService extends ChangeNotifier {episodeId: _currentEpisodeId,episodeTitle: _currentEpisodeTitle,libraryId: _currentLibraryId,+ fromUi: fromUi,);return;}@@ -6046,6 +6099,10 @@ class AudioPlayerService extends ChangeNotifier {_lastAccrualPos = null;// Start playback immediately — don't wait for server calls_player?.play();+ // Any paused seek up to this instant belonged to this resume (a socket+ // adoption can land between the check above and here); the flag is for+ // seeks during the NEXT pause.+ _seekedWhilePaused = false;_scheduleAudioDiagnostics('resume');_logEvent(PlaybackEventType.play, detail: logDetail);_onPlaybackStateChangedCallback?.call(true);@@ -6080,11 +6137,81 @@ class AudioPlayerService extends ChangeNotifier {notifyListeners();}- /// Re-create server session and check if server progress is ahead.- /// Runs in the background so play() returns instantly.+ /// Pause long enough that another device could plausibly have listened+ /// meanwhile. Shorter pauses resume instantly with no server round trip.+ static const _serverPositionCheckMinPause = Duration(minutes: 2);++ /// Longest an app-screen resume waits on the server before starting where+ /// the phone left off. A late answer is dropped, never seeked to.+ static const _serverPositionCheckCap = Duration(seconds: 2);++ /// Before an app-screen resume, look at the server's saved progress and,+ /// if another device genuinely moved it ahead, start there. Returns true+ /// when the position was adopted. Runs only before audio, capped, and only+ /// after a pause long enough to matter; anything slower starts local.+ Future<bool> _adoptServerPositionBeforeResume() async {+ if (_api == null || _currentItemId == null || _player == null) return false;+ // An idle player is about to be re-initialised through playItem, which+ // reconciles the start position itself.+ if (_player!.processingState == ProcessingState.idle) return false;+ if (_knownOffline || _isOfflineMode) return false;+ if (ChromecastService().isCasting) return false;+ final lastPause = _lastPauseTime;+ if (lastPause == null ||+ DateTime.now().difference(lastPause) < _serverPositionCheckMinPause) {+ return false;+ }+ final manualOffline =+ (_prefs ?? await SharedPreferences.getInstance()).getBool(+ 'manual_offline_mode',+ ) ??+ false;+ if (manualOffline) return false;+ final pKey = _currentEpisodeId != null+ ? '$_currentItemId-$_currentEpisodeId'+ : _currentItemId!;+ try {+ final localTs = await _progressSync.getSavedTimestamp(pKey);+ final serverProgress = await _api!+ .getItemProgress(pKey)+ .timeout(_serverPositionCheckCap, onTimeout: () => null);+ if (serverProgress == null) {+ debugPrint(+ '[Player] Resume server-check (pre-start): no answer within ${_serverPositionCheckCap.inMilliseconds}ms - starting local',+ );+ return false;+ }+ final serverPos = (serverProgress['currentTime'] as num?)?.toDouble() ?? 0;+ final serverTs = (serverProgress['lastUpdate'] as num?)?.toInt() ?? 0;+ final localPos = position.inMilliseconds / 1000.0;+ // Timestamp gate, like the sync path: our own pre-rewind position still+ // sits on the server after a resume, so position alone would call every+ // auto-rewind "server ahead".+ final ahead = serverTs > localTs && serverPos > localPos + 5.0;+ debugPrint(+ '[Player] Resume server-check (pre-start): server=${serverPos}s(ts=$serverTs) vs local=${localPos}s(ts=$localTs) -> ${ahead ? "SEEKING" : "keep local"}',+ );+ if (!ahead) return false;+ await _seekAbsolute(serverPos);+ _logEvent(+ PlaybackEventType.seek,+ detail: 'adopted server position ${serverPos.toStringAsFixed(1)}s before resume',+ );+ return true;+ } catch (e) {+ debugPrint('[Player] Resume server-check (pre-start) failed: $e');+ return false;+ }+ }++ /// Re-create the server session after a pause long enough to have closed+ /// it. Runs in the background so play() returns instantly. Position is not+ /// touched here: once audio is running we never seek it out from under the+ /// listener (see play()).void _resumeServerSync() async {if (_api == null || _currentItemId == null) return;if (_recreatingSession) return;+ if (_playbackSessionId != null) return;final manualOffline =(_prefs ?? await SharedPreferences.getInstance()).getBool('manual_offline_mode',@@ -6096,79 +6223,23 @@ class AudioPlayerService extends ChangeNotifier {);return;}- // Skip server position override if user manually seeked while paused- // (e.g. jumped to a different chapter) — respect the intentional seek.- final skipOverride = _seekedWhilePaused;- _seekedWhilePaused = false;- final pKey = _currentEpisodeId != null- ? '$_currentItemId-$_currentEpisodeId'- : _currentItemId!;_recreatingSession = true;try {- // Only let the server pull us forward if its progress is genuinely newer- // (another device advanced it). Position alone is wrong right after an- // auto-rewind: our pre-rewind position still sits on the server, so a big- // rewind looks like "server ahead" and gets erased. Gate on timestamp,- // like the sync path, since _lastAutoRewindAmount only covers one rewind.- final localTs = await _progressSync.getSavedTimestamp(pKey);- if (_playbackSessionId == null) {- // Session expired - re-create it- final sessionData = _currentEpisodeId != null- ? await _api!.startEpisodePlaybackSession(- _currentItemId!,- _currentEpisodeId!,- )- : await _api!.startPlaybackSession(_currentItemId!);- if (sessionData != null) {- _playbackSessionId = sessionData['id'] as String?;- debugPrint(- '[Player] Re-created session on resume: $_playbackSessionId',- );- _stashSessionUpgrade(sessionData);- if (!skipOverride) {- final serverPos =- (sessionData['currentTime'] as num?)?.toDouble() ?? 0;- final serverTs = (sessionData['updatedAt'] as num?)?.toInt() ?? 0;- final localPos = position.inMilliseconds / 1000.0;- final ahead =- serverTs > localTs &&- serverPos > localPos + _lastAutoRewindAmount + 5.0;- // [AlphaDiag] log inputs; strip before beta.- debugPrint(- '[Player] Resume server-check (recreated): server=${serverPos}s(ts=$serverTs) vs local=${localPos}s(ts=$localTs) rewindAmt=$_lastAutoRewindAmount -> ${ahead ? "SEEKING" : "keep local"}',- );- if (ahead) {- await _seekAbsolute(serverPos);- }- }- }- } else {- // Session still active - check server progress in case another client advanced- if (!skipOverride) {- final serverProgress = await _api!.getItemProgress(pKey);- if (serverProgress != null) {- final serverPos =- (serverProgress['currentTime'] as num?)?.toDouble() ?? 0;- final serverTs =- (serverProgress['lastUpdate'] as num?)?.toInt() ?? 0;- final localPos = position.inMilliseconds / 1000.0;- final ahead =- serverTs > localTs &&- serverPos > localPos + _lastAutoRewindAmount + 5.0;- // [AlphaDiag] log the inputs so we can tell a stale pre-rewind server- // position from a real cross-device advance. Strip before beta.- debugPrint(- '[Player] Resume server-check (active): server=${serverPos}s(ts=$serverTs) vs local=${localPos}s(ts=$localTs) rewindAmt=$_lastAutoRewindAmount -> ${ahead ? "SEEKING" : "keep local"}',- );- if (ahead) {- await _seekAbsolute(serverPos);- }- }- }+ final sessionData = _currentEpisodeId != null+ ? await _api!.startEpisodePlaybackSession(+ _currentItemId!,+ _currentEpisodeId!,+ )+ : await _api!.startPlaybackSession(_currentItemId!);+ if (sessionData != null) {+ _playbackSessionId = sessionData['id'] as String?;+ debugPrint(+ '[Player] Re-created session on resume: $_playbackSessionId',+ );+ _stashSessionUpgrade(sessionData);}- _lastAutoRewindAmount = 0;} catch (e) {- debugPrint('[Player] Failed to check server progress on resume: $e');+ debugPrint('[Player] Failed to re-create session on resume: $e');} finally {_recreatingSession = false;}@@ -6272,12 +6343,12 @@ class AudioPlayerService extends ChangeNotifier {});}- Future<void> togglePlayPause() async {+ Future<void> togglePlayPause({bool fromUi = false}) async {debugPrint('[Service] togglePlayPause() — isPlaying=$isPlaying');if (isPlaying) {await pause();} else {- await play();+ await play(fromUi: fromUi);}}diff --git a/lib/services/home_widget_service.dart b/lib/services/home_widget_service.dartindex f76fd5cc..f1c09317 100644--- a/lib/services/home_widget_service.dart+++ b/lib/services/home_widget_service.dart@@ -422,6 +422,13 @@ class HomeWidgetService {final episodeId = prefs.getString('widget_episode_id');try {+ // A downloaded item needs nothing from the server to start: the download+ // record already carries title/author/duration/chapters, so play it now+ // instead of holding a headphone / widget press behind getLibraryItem+ // (15s timeout, and no playback at all if it failed - GH #321).+ if (await _resumeFromDownloadRecord(player, api, itemId, episodeId)) {+ return;+ }debugPrint('[HomeWidget] play_pause: fetching item $itemId (episode=$episodeId)',);@@ -480,6 +487,51 @@ class HomeWidgetService {}}+ /// Cold-resume a downloaded item straight from its download record. Returns+ /// false when the item isn't downloaded (or the record is unusable) so the+ /// caller falls back to fetching it from the server.+ Future<bool> _resumeFromDownloadRecord(+ AudioPlayerService player,+ ApiService api,+ String itemId,+ String? episodeId,+ ) async {+ final downloads = DownloadService();+ final dlKey = episodeId != null ? '$itemId-$episodeId' : itemId;+ if (!downloads.isDownloaded(dlKey)) return false;+ final info = downloads.getInfo(dlKey);+ if (info.localPaths.isEmpty) return false;+ double duration = 0;+ List<dynamic> chapters = const [];+ final sessionJson = info.sessionData;+ if (sessionJson != null) {+ try {+ final session = jsonDecode(sessionJson) as Map<String, dynamic>;+ duration = (session['duration'] as num?)?.toDouble() ?? 0;+ chapters = session['chapters'] as List<dynamic>? ?? const [];+ } catch (_) {}+ }+ final title = info.title ?? '';+ debugPrint(+ '[HomeWidget] play_pause: downloaded, resuming from record (title="$title" episode=$episodeId dur=${duration.toStringAsFixed(0)}s)',+ );+ // Episode downloads store the episode as title and the show as author,+ // which is exactly what playItem wants for a podcast episode.+ await player.playItem(+ api: api,+ itemId: itemId,+ title: title,+ author: info.author ?? '',+ coverUrl: api.getCoverUrl(itemId),+ totalDuration: duration,+ chapters: episodeId != null ? const [] : chapters,+ episodeId: episodeId,+ episodeTitle: episodeId != null ? title : null,+ libraryId: info.libraryId,+ );+ return true;+ }+void _onPlayerChanged() {// Throttle to max once per 2 seconds, but never drop an update —// schedule a deferred one so the final state always gets pushed.diff --git a/lib/services/quick_actions_service.dart b/lib/services/quick_actions_service.dartindex 1b1a6313..99630702 100644--- a/lib/services/quick_actions_service.dart+++ b/lib/services/quick_actions_service.dart@@ -105,7 +105,7 @@ class QuickActionsService {// and early-returns when player.hasBook is true, so we can't reuse it.if (player.hasBook) {if (!player.isPlaying) {- await player.play();+ await player.play(fromUi: true);}return;}diff --git a/lib/widgets/absorbing_card.dart b/lib/widgets/absorbing_card.dartindex a2442812..203dd403 100644--- a/lib/widgets/absorbing_card.dart+++ b/lib/widgets/absorbing_card.dart@@ -899,7 +899,7 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCliif (isCastingThis) {castService.togglePlayPause();} else if (_isActive) {- widget.player.togglePlayPause();+ widget.player.togglePlayPause(fromUi: true);} else {_startPlayback();}@@ -1263,6 +1263,7 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCliepisodeId: _episodeId,episodeTitle: _recentEpisode?['title'] as String?,libraryId: _resolveLibraryId(),+ fromUi: true,);if (mounted) {if (error != null) showErrorToast(context, error);diff --git a/lib/widgets/book_detail_sheet.dart b/lib/widgets/book_detail_sheet.dartindex 6284baff..1be8ed3b 100644--- a/lib/widgets/book_detail_sheet.dart+++ b/lib/widgets/book_detail_sheet.dart@@ -2187,7 +2187,7 @@ class _BookDetailSheetContentState extends State<_BookDetailSheetContent> {if (player.currentItemId == widget.itemId) {await _activateQueueSource();- if (!player.isPlaying) await player.play();+ if (!player.isPlaying) await player.play(fromUi: true);unawaited(lib.syncQueueAutoDownloads());rootNav.popUntil((route) => route.isFirst);Future.delayed(const Duration(milliseconds: 100), () {@@ -2206,7 +2206,7 @@ class _BookDetailSheetContentState extends State<_BookDetailSheetContent> {rootNav.popUntil((route) => route.isFirst);AppShell.goToAbsorbingGlobal();- final error = await player.playItem(api: api, itemId: widget.itemId, title: title, author: author, coverUrl: coverUrl, totalDuration: duration, chapters: chapters, libraryId: _item?['libraryId'] as String?);+ final error = await player.playItem(api: api, itemId: widget.itemId, title: title, author: author, coverUrl: coverUrl, totalDuration: duration, chapters: chapters, libraryId: _item?['libraryId'] as String?, fromUi: true);if (error != null) {unawaited(_restoreActivatedQueueSource(queueModeBackup));final ctx = rootNavigatorKey.currentContext;diff --git a/lib/widgets/ebook_reader_view.dart b/lib/widgets/ebook_reader_view.dartindex 5fe17553..701447ca 100644--- a/lib/widgets/ebook_reader_view.dart+++ b/lib/widgets/ebook_reader_view.dart@@ -249,6 +249,7 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObstotalDuration: _audioDuration,chapters: _audioChapters,libraryId: lib.selectedLibraryId,+ fromUi: true,);if (mounted) setState(() => _startingAudio = false);}diff --git a/lib/widgets/episode_detail_sheet.dart b/lib/widgets/episode_detail_sheet.dartindex 3561fe8a..43ce619d 100644--- a/lib/widgets/episode_detail_sheet.dart+++ b/lib/widgets/episode_detail_sheet.dart@@ -185,6 +185,7 @@ class _EpisodeDetailSheetState extends State<EpisodeDetailSheet> {// The show's own library, not the browsing library (wrong with unified// libraries on). Null lets the player resolve it from the session.libraryId: widget.podcastItem['libraryId'] as String?,+ fromUi: true,);debugPrint('[PodcastPlay] playItem returned in ${DateTime.now().difference(t0).inMilliseconds}ms (error=${error ?? 'none'})');if (error != null) {diff --git a/lib/widgets/episode_list_sheet.dart b/lib/widgets/episode_list_sheet.dartindex 33f60efa..e4787877 100644--- a/lib/widgets/episode_list_sheet.dart+++ b/lib/widgets/episode_list_sheet.dart@@ -520,6 +520,7 @@ class _EpisodeListSheetState extends State<EpisodeListSheet> {// selected library would pin the wrong per-library skip amounts. Null// is fine: the player resolves it from the session/server.libraryId: _podcastItem['libraryId'] as String?,+ fromUi: true,);if (error == null) {unawaited(lib.syncQueueAutoDownloads());diff --git a/lib/widgets/expanded_card.dart b/lib/widgets/expanded_card.dartindex 53fa242c..8f09bb12 100644--- a/lib/widgets/expanded_card.dart+++ b/lib/widgets/expanded_card.dart@@ -788,7 +788,7 @@ class _ExpandedCardState extends State<ExpandedCard> {if (isCastingThis) {castService.togglePlayPause();} else if (_isActive) {- widget.player.togglePlayPause();+ widget.player.togglePlayPause(fromUi: true);} else {_startPlayback();}@@ -1142,6 +1142,7 @@ class _ExpandedCardState extends State<ExpandedCard> {episodeId: _episodeId,episodeTitle: _recentEpisode?['title'] as String?,libraryId: _resolveLibraryId(),+ fromUi: true,);if (mounted) {if (error != null) showErrorToast(context, error);diff --git a/lib/widgets/podcast_episode_feed.dart b/lib/widgets/podcast_episode_feed.dartindex b1495b06..0565cc80 100644--- a/lib/widgets/podcast_episode_feed.dart+++ b/lib/widgets/podcast_episode_feed.dart@@ -375,6 +375,7 @@ class _PodcastEpisodeFeedState extends State<PodcastEpisodeFeed> {libraryId: ep['libraryId'] as String? ??(ep['podcast'] as Map<String, dynamic>?)?['libraryId'] as String? ??widget.libraryId,+ fromUi: true,);if (error != null && mounted) showErrorToast(context, error);}