A cross platform Audiobookshelf client for Android and iOS
Dart
Fix iOS background queue, attempt 8 - pre-buffer next book in queue
Barnabasdd6a7d170644
diff --git a/lib/providers/_lp_absorbing.dart b/lib/providers/_lp_absorbing.dartindex d8ebecc0..2933976d 100644--- a/lib/providers/_lp_absorbing.dart+++ b/lib/providers/_lp_absorbing.dart@@ -1163,6 +1163,84 @@ mixin _AbsorbingMixin on ChangeNotifier, _StateMixin, _CoreMixin {return metadata['title'] as String?;}+ /// Returns metadata for the item that would auto-advance next in manual+ /// queue mode, formatted for AudioPlayerService's pre-buffer mechanism.+ /// Returns null when nothing's queued, mode isn't manual, or the next item+ /// isn't downloaded (MVP only pre-buffers local files).+ Future<Map<String, dynamic>?> peekNextQueueItemForPreBuffer(+ String currentItemId) async {+ final self = this as LibraryProvider;+ final isPodCurrent = currentItemId.length > 36;+ final mode = isPodCurrent+ ? await PlayerSettings.getPodcastQueueMode()+ : await PlayerSettings.getBookQueueMode();+ if (mode != 'manual') return null;++ final bookMode = await PlayerSettings.getBookQueueMode();+ final podMode = await PlayerSettings.getPodcastQueueMode();+ final merged = await PlayerSettings.getMergeAbsorbingLibraries();+ final currentLibId =+ _absorbingItemCache[currentItemId]?['libraryId'] as String?;+ final idx = _absorbingBookIds.indexOf(currentItemId);+ final start = idx >= 0 ? idx + 1 : 0;++ for (var i = start; i < _absorbingBookIds.length; i++) {+ final key = _absorbingBookIds[i];+ if (self.isItemFinishedByKey(key)) continue;+ final candidateIsPodcast = key.length > 36;+ final candidateMode = candidateIsPodcast ? podMode : bookMode;+ if (candidateMode == 'off') continue;+ if (!merged && candidateIsPodcast != isPodCurrent) continue;+ final cached = _absorbingItemCache[key];+ if (cached == null) continue;+ if (!merged && currentLibId != null) {+ final candidateLibId = cached['libraryId'] as String?;+ if (candidateLibId != null && candidateLibId != currentLibId) continue;+ }++ final localPaths = DownloadService().getLocalPaths(key);+ if (localPaths == null || localPaths.isEmpty) {+ debugPrint('[PreBuffer] Next item $key not downloaded, skip');+ return null;+ }+ if (localPaths.length != 1) {+ debugPrint('[PreBuffer] Next item $key is multi-track, skip (MVP)');+ return null;+ }++ final itemIdRaw = candidateIsPodcast ? key.substring(0, 36) : key;+ final episodeIdRaw = candidateIsPodcast ? key.substring(37) : null;+ final media = cached['media'] as Map<String, dynamic>? ?? {};+ final metadata = media['metadata'] as Map<String, dynamic>? ?? {};+ final title = candidateIsPodcast+ ? ((cached['recentEpisode'] as Map<String, dynamic>?)?['title']+ as String? ??+ metadata['title'] as String? ??+ '')+ : (metadata['title'] as String? ?? '');+ final author = metadata['authorName'] as String? ?? '';+ final duration = candidateIsPodcast+ ? (((cached['recentEpisode'] as Map<String, dynamic>?)?['duration']+ as num?)+ ?.toDouble() ??+ (media['duration'] as num?)?.toDouble() ??+ 0)+ : ((media['duration'] as num?)?.toDouble() ?? 0);+ final chapters = (media['chapters'] as List<dynamic>?) ?? const [];+ return {+ 'itemId': itemIdRaw,+ 'episodeId': episodeIdRaw,+ 'title': title,+ 'author': author,+ 'coverUrl': getCoverUrl(itemIdRaw),+ 'duration': duration,+ 'chapters': chapters,+ 'localPaths': localPaths,+ };+ }+ return null;+ }+/// Returns a short label describing what would play next given the current/// queue mode and player state, or null when nothing is queued. Used by the/// "Up next: ..." chip under the queue-mode pill on the absorbing page.diff --git a/lib/providers/library_provider.dart b/lib/providers/library_provider.dartindex bf7a0cec..4e54dd39 100644--- a/lib/providers/library_provider.dart+++ b/lib/providers/library_provider.dart@@ -32,6 +32,13 @@ class LibraryProvider extends ChangeNotifier// Flutter UI wasn't foregrounded yet (e.g. Android Auto cold-start,// where updateAuth may not run until much later).AudioPlayerService.setOnBookFinishedCallback(markFinishedLocally);+ AudioPlayerService.setOnAutoQueueAdvancedCallback((oldItemId) {+ // Pre-buffer auto-advance already loaded and started the next item.+ // Mark the old one finished WITHOUT triggering normal auto-advance+ // (which would call playItem again and rebuild the source).+ markFinishedLocally(oldItemId, skipAutoAdvance: true);+ });+ AudioPlayerService.setOnPeekNextItemCallback(peekNextQueueItemForPreBuffer);AudioPlayerService.setOnPlayStartedCallback((key) {Future.delayed(const Duration(seconds: 30), () {final stillPlaying = AudioPlayerService().isPlaying;diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dartindex 30176534..0e8eec1a 100644--- a/lib/services/audio_player_service.dart+++ b/lib/services/audio_player_service.dart@@ -809,6 +809,27 @@ class AudioPlayerService extends ChangeNotifier {_onBookFinishedCallback = cb;}+ // Pre-buffer callback. AudioPlayerService asks LibraryProvider for the next+ // manual-queue item ~15s before current ends, so the next AVQueuePlayer item+ // is already in the queue when the current one finishes. iOS auto-advances+ // natively (no fresh setAudioSource in background, so iOS keeps granting+ // audio output). The map must contain at least: itemId, title, author,+ // duration, and either localPaths or audioTracks.+ static Future<Map<String, dynamic>?> Function(String currentItemId)? _onPeekNextItemCallback;+ static void setOnPeekNextItemCallback(+ Future<Map<String, dynamic>?> Function(String)? cb) {+ _onPeekNextItemCallback = cb;+ }++ // Fired when the AVQueuePlayer natively auto-advances from the current item+ // to a pre-buffered next item. LibraryProvider uses this to mark the old+ // item finished WITHOUT triggering its normal auto-advance flow (which would+ // call playItem and rebuild the source, defeating the pre-buffer).+ static void Function(String oldItemId)? _onAutoQueueAdvancedCallback;+ static void setOnAutoQueueAdvancedCallback(void Function(String)? cb) {+ _onAutoQueueAdvancedCallback = cb;+ }+static void drainPendingBookFinished() {final cb = _onBookFinishedCallback;final pending = _pendingBookFinishedKey;@@ -900,6 +921,17 @@ class AudioPlayerService extends ChangeNotifier {SharedPreferences? _prefs;StreamSubscription? _syncSub;StreamSubscription? _completionSub;++ // ── Pre-buffer next book in queue (iOS background auto-advance fix) ──+ // iOS denies background audio output to freshly-loaded AVPlayerItems.+ // Workaround: append the next book to the live ConcatenatingAudioSource+ // ~15s before current ends. AVQueuePlayer auto-advances natively, audio+ // engine stays continuous, iOS keeps granting output.+ ConcatenatingAudioSource? _activeConcatSource;+ int _currentBookTrackCount = 0;+ bool _nextBookPreloading = false;+ Map<String, dynamic>? _preloadedNextBook;+ bool _autoQueueAdvancing = false;Timer? _bgSaveTimer;Timer? _pauseStopTimer;static const _pauseStopTimeout = Duration(minutes: 10);@@ -1051,6 +1083,139 @@ class AudioPlayerService extends ChangeNotifier {static const _eqChannelForDiag = MethodChannel('com.absorb.equalizer');+ void _resetPreBufferState() {+ _activeConcatSource = null;+ _currentBookTrackCount = 0;+ _nextBookPreloading = false;+ _preloadedNextBook = null;+ }++ void _maybePreloadNextBook() {+ if (_nextBookPreloading) return;+ if (_preloadedNextBook != null) return;+ if (_activeConcatSource == null) return;+ if (_currentBookTrackCount == 0) return;+ if (_totalDuration <= 0) return;+ final remaining = _totalDuration - _lastKnownPositionSec;+ if (remaining <= 0 || remaining > 15) return;+ if (_currentItemId == null) return;+ final cb = _onPeekNextItemCallback;+ if (cb == null) return;++ _nextBookPreloading = true;+ final currentId = _currentEpisodeId != null+ ? '$_currentItemId-$_currentEpisodeId'+ : _currentItemId!;+ unawaited(_preloadNextBookAsync(cb, currentId));+ }++ Future<void> _preloadNextBookAsync(+ Future<Map<String, dynamic>?> Function(String) cb, String currentId) async {+ try {+ final next = await cb(currentId);+ if (next == null) {+ debugPrint('[PreBuffer] No next item to preload');+ return;+ }+ final nextItemId = next['itemId'] as String?;+ if (nextItemId == null) {+ debugPrint('[PreBuffer] Next item has no itemId');+ return;+ }+ // Build audio source for the next item.+ final localPaths = (next['localPaths'] as List<dynamic>?)?.cast<String>();+ final audioTracks = next['audioTracks'] as List<dynamic>?;+ final audioHeaders = next['audioHeaders'] as Map<String, String>?;++ List<AudioSource>? nextTrackSources;+ if (localPaths != null && localPaths.isNotEmpty) {+ nextTrackSources =+ localPaths.map((p) => AudioSource.file(p) as AudioSource).toList();+ } else if (audioTracks != null && audioTracks.isNotEmpty) {+ nextTrackSources = audioTracks+ .map((t) => AudioSource.uri(+ Uri.parse((t as Map<String, dynamic>)['url'] as String),+ headers: audioHeaders,+ ) as AudioSource)+ .toList();+ }+ if (nextTrackSources == null || nextTrackSources.isEmpty) {+ debugPrint('[PreBuffer] No playable sources for next item');+ return;+ }+ if (nextTrackSources.length != 1) {+ debugPrint('[PreBuffer] Next item is multi-track, skipping (MVP supports single-track only)');+ return;+ }++ final concat = _activeConcatSource;+ if (concat == null) {+ debugPrint('[PreBuffer] Concat source went away mid-preload');+ return;+ }+ for (final s in nextTrackSources) {+ await concat.add(s);+ }+ _preloadedNextBook = next;+ debugPrint('[PreBuffer] Pre-loaded next item: ${next['title']} ($nextItemId)');+ } catch (e, st) {+ debugPrint('[PreBuffer] Failed: $e\n$st');+ }+ // _nextBookPreloading stays true regardless of outcome — single attempt per+ // play session. Reset happens in _resetPreBufferState on the next playItem.+ }++ void _onAutoQueueAdvanced() {+ final next = _preloadedNextBook;+ if (next == null) return;+ if (_autoQueueAdvancing) return;+ _autoQueueAdvancing = true;+ debugPrint('[PreBuffer] Auto-queue advanced to ${next['title']}');++ final oldItemId = _currentItemId;+ final oldEpisodeId = _currentEpisodeId;+ final oldKey = oldEpisodeId != null ? '$oldItemId-$oldEpisodeId' : oldItemId;++ // Promote next book's metadata to current state+ _currentItemId = next['itemId'] as String?;+ _currentEpisodeId = next['episodeId'] as String?;+ _currentTitle = next['title'] as String?;+ _currentAuthor = next['author'] as String?;+ _currentCoverUrl = next['coverUrl'] as String?;+ _totalDuration = (next['duration'] as num?)?.toDouble() ?? 0;+ _chapters = (next['chapters'] as List<dynamic>?) ?? [];+ _handler?.updateChaptersQueue(_chapters);+ _trackStartOffsets = [0.0, _totalDuration];+ _currentTrackIndex = 0;+ _playbackSessionId = null;+ _lastKnownPositionSec = 0;+ _lastNotifiedChapterIndex = -1;+ _currentBookTrackCount = 1;+ _preloadedNextBook = null;+ _nextBookPreloading = false;++ // Push new media item to update Now Playing / lock screen+ final initChapter = _initChapterInfo(0);+ _pushMediaItem(_currentItemId!, _currentTitle ?? '', _currentAuthor ?? '',+ _currentCoverUrl, _totalDuration,+ chapter: initChapter);+ unawaited(_primeNowPlaying(+ title: _currentTitle ?? '',+ artist: _currentAuthor ?? '',+ duration: _totalDuration,+ elapsed: 0));++ // Notify LibraryProvider via auto-queue-specific callback (skipAutoAdvance+ // inside markFinishedLocally — next item is already playing, no playItem).+ if (oldKey != null) {+ final cb = _onAutoQueueAdvancedCallback;+ if (cb != null) cb(oldKey);+ }++ notifyListeners();+ _autoQueueAdvancing = false;+ }+Future<void> _primeNowPlaying({required String title,required String artist,@@ -1215,9 +1380,21 @@ class AudioPlayerService extends ChangeNotifier {/// Subscribe to track index changes for multi-file playback.void _subscribeTrackIndex() {_indexSub?.cancel();- if (_player == null || _trackStartOffsets.length <= 1) return;+ if (_player == null) return;_indexSub = _player!.currentIndexStream.listen((index) {- if (index != null) {+ if (index == null) return;+ // Pre-buffer auto-advance: if the queue has grown past the current+ // book's tracks and AVQueuePlayer has stepped into the pre-loaded+ // next book's range, fire the book transition.+ if (_currentBookTrackCount > 0 &&+ index >= _currentBookTrackCount &&+ _preloadedNextBook != null) {+ debugPrint('[PreBuffer] currentIndex=$index crossed book boundary at $_currentBookTrackCount');+ _onAutoQueueAdvanced();+ return;+ }+ // Within-book multi-track advance (existing behavior).+ if (_trackStartOffsets.length > 1) {final clamped = index.clamp(0, _trackStartOffsets.length - 2);if (clamped != _currentTrackIndex) {_lastIndexAdvanceTime = DateTime.now();@@ -1999,15 +2176,10 @@ class AudioPlayerService extends ChangeNotifier {_trackStartOffsets = [0.0]; // single file fallback}- AudioSource source;- if (localPaths.length == 1) {- source = AudioSource.file(localPaths.first);- } else {- final sources = localPaths- .map((p) => AudioSource.file(p) as AudioSource)- .toList();- source = ConcatenatingAudioSource(children: sources);- }+ final trackSources = localPaths+ .map((p) => AudioSource.file(p) as AudioSource)+ .toList();+ final source = ConcatenatingAudioSource(children: trackSources);await _configureAudioSession();try {@@ -2016,7 +2188,10 @@ class AudioPlayerService extends ChangeNotifier {} catch (e) {debugPrint('[Player] Pre-source setActive failed (local): $e');}+ _resetPreBufferState();await _player!.setAudioSource(source);+ _activeConcatSource = source;+ _currentBookTrackCount = trackSources.length;// If the saved position is at (or past) the end, restart from the beginningif (totalDuration > 0 && startTime >= totalDuration - 1.0) startTime = 0;@@ -2108,21 +2283,14 @@ class AudioPlayerService extends ChangeNotifier {_buildTrackOffsets(audioTracks);_captureStreamUrls(audioTracks, api);AudioSource source;- if (audioTracks.length == 1) {- final track = audioTracks.first as Map<String, dynamic>;+ final trackSources = <AudioSource>[];+ for (final t in audioTracks) {+ final track = t as Map<String, dynamic>;final contentUrl = track['contentUrl'] as String? ?? '';final fullUrl = api.buildTrackUrl(contentUrl);- source = AudioSource.uri(Uri.parse(fullUrl), headers: audioHeaders);- } else {- final sources = <AudioSource>[];- for (final t in audioTracks) {- final track = t as Map<String, dynamic>;- final contentUrl = track['contentUrl'] as String? ?? '';- final fullUrl = api.buildTrackUrl(contentUrl);- sources.add(AudioSource.uri(Uri.parse(fullUrl), headers: audioHeaders));- }- source = ConcatenatingAudioSource(children: sources);+ trackSources.add(AudioSource.uri(Uri.parse(fullUrl), headers: audioHeaders));}+ source = ConcatenatingAudioSource(children: trackSources);await _configureAudioSession();try {@@ -2131,7 +2299,10 @@ class AudioPlayerService extends ChangeNotifier {} catch (e) {debugPrint('[Player] Pre-source setActive failed (cached-session): $e');}+ _resetPreBufferState();await _player!.setAudioSource(source);+ _activeConcatSource = source as ConcatenatingAudioSource;+ _currentBookTrackCount = trackSources.length;if (totalDuration > 0 && startTime >= totalDuration - 1.0) startTime = 0;if (startTime > 0) {@@ -2370,22 +2541,14 @@ class AudioPlayerService extends ChangeNotifier {// Build audio source - one source per track file_buildTrackOffsets(audioTracks);_captureStreamUrls(audioTracks, api);- AudioSource source;- if (audioTracks.length == 1) {- final track = audioTracks.first as Map<String, dynamic>;+ final trackSources = <AudioSource>[];+ for (final t in audioTracks) {+ final track = t as Map<String, dynamic>;final contentUrl = track['contentUrl'] as String? ?? '';final fullUrl = api.buildTrackUrl(contentUrl);- source = AudioSource.uri(Uri.parse(fullUrl), headers: audioHeaders);- } else {- final sources = <AudioSource>[];- for (final t in audioTracks) {- final track = t as Map<String, dynamic>;- final contentUrl = track['contentUrl'] as String? ?? '';- final fullUrl = api.buildTrackUrl(contentUrl);- sources.add(AudioSource.uri(Uri.parse(fullUrl), headers: audioHeaders));- }- source = ConcatenatingAudioSource(children: sources);+ trackSources.add(AudioSource.uri(Uri.parse(fullUrl), headers: audioHeaders));}+ final source = ConcatenatingAudioSource(children: trackSources);await _configureAudioSession();try {@@ -2394,7 +2557,10 @@ class AudioPlayerService extends ChangeNotifier {} catch (e) {debugPrint('[Player] Pre-source setActive failed (stream): $e');}+ _resetPreBufferState();await _player!.setAudioSource(source);+ _activeConcatSource = source;+ _currentBookTrackCount = trackSources.length;// If the saved position is at (or past) the end, restart from the beginningif (totalDuration > 0 && startTime >= totalDuration - 1.0) startTime = 0;@@ -2929,6 +3095,8 @@ class AudioPlayerService extends ChangeNotifier {}if (posSec > 0) _lastKnownPositionSec = posSec;+ _maybePreloadNextBook();+if (sec <= 0) return;// In background, only process once per second to save CPU.diff --git a/pubspec.yaml b/pubspec.yamlindex e4b1916d..26c5f073 100644--- a/pubspec.yaml+++ b/pubspec.yaml@@ -1,7 +1,7 @@name: absorbdescription: An Audiobookshelf Clientpublish_to: 'none'-version: 1.8.91+142+version: 1.8.91+143environment:sdk: '>=3.1.0 <4.0.0'