Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Fix iOS background queue, attempt 14 - stop broadcasting -1 index, correct elapsed

Barnabas98d2ecc711cc
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dart
index c1edf305..d1980c94 100644
--- a/lib/services/audio_player_service.dart
+++ b/lib/services/audio_player_service.dart
@@ -1209,6 +1209,18 @@ class AudioPlayerService extends ChangeNotifier {
debugPrint('[PreBuffer] Next item has no itemId');
return;
}
+ // Resolve the next book's resume position once and stash it on the map
+ // so commitAdvance, Now Playing, and _onAutoQueueAdvanced all agree.
+ double startS = 0;
+ final progressKey = next['episodeId'] != null
+ ? '$nextItemId-${next['episodeId']}'
+ : nextItemId;
+ final saved = await _progressSync.getLocal(progressKey);
+ if (saved != null && !(saved['isFinished'] as bool? ?? false)) {
+ startS = (saved['currentTime'] as num?)?.toDouble() ?? 0;
+ }
+ next['startS'] = startS;
+
// Build audio source for the next item.
final localPaths = (next['localPaths'] as List<dynamic>?)?.cast<String>();
final audioTracks = next['audioTracks'] as List<dynamic>?;
@@ -1281,16 +1293,7 @@ class AudioPlayerService extends ChangeNotifier {
if (local != null && local.isNotEmpty) coverPath = local;
}
- double startS = 0;
- if (itemId != null) {
- final progressKey = next['episodeId'] != null
- ? '$itemId-${next['episodeId']}'
- : itemId;
- final local = await _progressSync.getLocal(progressKey);
- if (local != null && !(local['isFinished'] as bool? ?? false)) {
- startS = (local['currentTime'] as num?)?.toDouble() ?? 0;
- }
- }
+ final startS = (next['startS'] as num?)?.toDouble() ?? 0;
final ok = await _queueAdvancerChannel.invokeMethod<bool>('prepareNext', {
'url': url,
@@ -1333,14 +1336,15 @@ class AudioPlayerService extends ChangeNotifier {
_trackStartOffsets = [0.0, _totalDuration];
_currentTrackIndex = 0;
_playbackSessionId = null;
- _lastKnownPositionSec = 0;
+ final startS = (next['startS'] as num?)?.toDouble() ?? 0;
+ _lastKnownPositionSec = startS;
_lastNotifiedChapterIndex = -1;
_currentBookTrackCount = 1;
_preloadedNextBook = null;
_nextBookPreloading = false;
// Push new media item to update Now Playing / lock screen
- final initChapter = _initChapterInfo(0);
+ final initChapter = _initChapterInfo(startS);
_pushMediaItem(_currentItemId!, _currentTitle ?? '', _currentAuthor ?? '',
_currentCoverUrl, _totalDuration,
chapter: initChapter);
@@ -1348,7 +1352,7 @@ class AudioPlayerService extends ChangeNotifier {
title: _currentTitle ?? '',
artist: _currentAuthor ?? '',
duration: _totalDuration,
- elapsed: 0));
+ elapsed: startS));
if (Platform.isIOS) {
unawaited(_iosAutoAdvanceKick());
diff --git a/packages/just_audio/darwin/just_audio/Sources/just_audio/AudioPlayer.m b/packages/just_audio/darwin/just_audio/Sources/just_audio/AudioPlayer.m
index c08303d4..e34b87c1 100644
--- a/packages/just_audio/darwin/just_audio/Sources/just_audio/AudioPlayer.m
+++ b/packages/just_audio/darwin/just_audio/Sources/just_audio/AudioPlayer.m
@@ -916,10 +916,9 @@
//IndexedPlayerItem *oldPlayerItem = (IndexedPlayerItem *)change[NSKeyValueChangeOldKey];
if (![playerItem isKindOfClass:[IndexedPlayerItem class]]) {
// Host-app code replaced currentItem with a vanilla AVPlayerItem.
- // Mark our internal index invalid and stop tracking until the host
- // calls load again.
- _index = -1;
- [self broadcastPlaybackEvent];
+ // Leave our internal index alone and don't broadcast — sending
+ // currentIndex=-1 would crash the Dart event handler that
+ // dereferences it into the audio-source array.
return;
}
if (playerItem.status == AVPlayerItemStatusFailed) {
@@ -936,8 +935,7 @@
} else {
int expectedIndex = [self indexForItem:playerItem];
if (expectedIndex < 0) {
- _index = -1;
- [self broadcastPlaybackEvent];
+ // Foreign item swapped in; skip broadcasting an invalid index.
return;
}
if (_index != expectedIndex) {
diff --git a/pubspec.yaml b/pubspec.yaml
index e9e79ed9..8b611e96 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,7 +1,7 @@
name: absorb
description: An Audiobookshelf Client
publish_to: 'none'
-version: 1.8.91+149
+version: 1.8.91+150
environment:
sdk: '>=3.1.0 <4.0.0'