Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Fix iOS background queue, attempt 3

Barnabas5d5bd42638d7
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dart
index 8e0f1be4..2eeb89ef 100644
--- a/lib/services/audio_player_service.dart
+++ b/lib/services/audio_player_service.dart
@@ -3227,8 +3227,6 @@ class AudioPlayerService extends ChangeNotifier {
debugPrint('[Player] Book complete: $_currentTitle');
_logEvent(PlaybackEventType.bookFinished);
- // Stop immediately to prevent ExoPlayer from seeking back to position 0
- // (which triggers position-stream events that look like a restart).
// Cancel subscriptions first so we don't process stale events.
_syncSub?.cancel();
_syncSub = null;
@@ -3236,7 +3234,20 @@ class AudioPlayerService extends ChangeNotifier {
_completionSub = null;
_bgSaveTimer?.cancel();
_bgSaveTimer = null;
- await _player?.stop();
+ // Android: stop() prevents ExoPlayer's phantom seek-to-0 on completion,
+ // which would fire position-stream events that look like a restart.
+ // iOS: stop() calls _setPlatformActive(false) which tears down the
+ // AVPlayer entirely. The rebuilt AVPlayer for the next item is not
+ // granted audio session privileges in background, so audio plays
+ // silently and lock screen controls vanish. pause() preserves the
+ // platform player + session — paired with darwinLoadControl's
+ // automaticallyWaitsToMinimizeStalling=false this gives gapless
+ // background auto-advance. GH #244.
+ if (Platform.isAndroid) {
+ await _player?.stop();
+ } else {
+ await _player?.pause();
+ }
// Mark as finished on the server (fire-and-forget to avoid blocking
// auto-advance — the local save below is the source of truth).