Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Resume queue books where you left off instead of starting over

Barnabas31efff6d2a2e
diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb
index a52ff55f..ce023a55 100644
--- a/lib/l10n/app_en.arb
+++ b/lib/l10n/app_en.arb
@@ -855,6 +855,9 @@
"concurrentDownloads": "Concurrent downloads",
"autoDownload": "Auto-download",
"autoDownloadSubtitle": "Enable per series, podcast, playlist or collection from their detail pages",
+ "autoDownloadEnabledFor": "Turned on for",
+ "autoDownloadEnabledForNone": "Nothing yet",
+ "autoDownloadSourceUnnamed": "Not loaded yet",
"keepNext": "Keep next",
"keepNextInfoTitle": "Keep Next",
"keepNextInfoContent": "The number of items to keep downloaded, including the one you're currently listening to. For example, \"Keep next 3\" means the current book plus the next 2 in that series, podcast, playlist or collection will stay downloaded. This applies to every auto-download you turn on.",
diff --git a/lib/l10n/app_localizations.dart b/lib/l10n/app_localizations.dart
index 85309a6b..91794d6a 100644
--- a/lib/l10n/app_localizations.dart
+++ b/lib/l10n/app_localizations.dart
@@ -3310,6 +3310,24 @@ abstract class AppLocalizations {
/// **'Enable per series, podcast, playlist or collection from their detail pages'**
String get autoDownloadSubtitle;
+ /// No description provided for @autoDownloadEnabledFor.
+ ///
+ /// In en, this message translates to:
+ /// **'Turned on for'**
+ String get autoDownloadEnabledFor;
+
+ /// No description provided for @autoDownloadEnabledForNone.
+ ///
+ /// In en, this message translates to:
+ /// **'Nothing yet'**
+ String get autoDownloadEnabledForNone;
+
+ /// No description provided for @autoDownloadSourceUnnamed.
+ ///
+ /// In en, this message translates to:
+ /// **'Not loaded yet'**
+ String get autoDownloadSourceUnnamed;
+
/// No description provided for @keepNext.
///
/// In en, this message translates to:
diff --git a/lib/l10n/app_localizations_de.dart b/lib/l10n/app_localizations_de.dart
index 770c3aad..a748da80 100644
--- a/lib/l10n/app_localizations_de.dart
+++ b/lib/l10n/app_localizations_de.dart
@@ -1791,6 +1791,15 @@ class AppLocalizationsDe extends AppLocalizations {
String get autoDownloadSubtitle =>
'Pro Serie oder Podcast über deren Detailseiten aktivieren';
+ @override
+ String get autoDownloadEnabledFor => 'Turned on for';
+
+ @override
+ String get autoDownloadEnabledForNone => 'Nothing yet';
+
+ @override
+ String get autoDownloadSourceUnnamed => 'Not loaded yet';
+
@override
String get keepNext => 'Nächste behalten';
diff --git a/lib/l10n/app_localizations_el.dart b/lib/l10n/app_localizations_el.dart
index 32ce6d20..cade09cf 100644
--- a/lib/l10n/app_localizations_el.dart
+++ b/lib/l10n/app_localizations_el.dart
@@ -1775,6 +1775,15 @@ class AppLocalizationsEl extends AppLocalizations {
String get autoDownloadSubtitle =>
'Enable per series, podcast, playlist or collection from their detail pages';
+ @override
+ String get autoDownloadEnabledFor => 'Turned on for';
+
+ @override
+ String get autoDownloadEnabledForNone => 'Nothing yet';
+
+ @override
+ String get autoDownloadSourceUnnamed => 'Not loaded yet';
+
@override
String get keepNext => 'Keep next';
diff --git a/lib/l10n/app_localizations_en.dart b/lib/l10n/app_localizations_en.dart
index dac02eed..cd99a536 100644
--- a/lib/l10n/app_localizations_en.dart
+++ b/lib/l10n/app_localizations_en.dart
@@ -1775,6 +1775,15 @@ class AppLocalizationsEn extends AppLocalizations {
String get autoDownloadSubtitle =>
'Enable per series, podcast, playlist or collection from their detail pages';
+ @override
+ String get autoDownloadEnabledFor => 'Turned on for';
+
+ @override
+ String get autoDownloadEnabledForNone => 'Nothing yet';
+
+ @override
+ String get autoDownloadSourceUnnamed => 'Not loaded yet';
+
@override
String get keepNext => 'Keep next';
diff --git a/lib/l10n/app_localizations_es.dart b/lib/l10n/app_localizations_es.dart
index 69b4b7fa..1b170499 100644
--- a/lib/l10n/app_localizations_es.dart
+++ b/lib/l10n/app_localizations_es.dart
@@ -1777,6 +1777,15 @@ class AppLocalizationsEs extends AppLocalizations {
String get autoDownloadSubtitle =>
'Enable per series, podcast, playlist or collection from their detail pages';
+ @override
+ String get autoDownloadEnabledFor => 'Turned on for';
+
+ @override
+ String get autoDownloadEnabledForNone => 'Nothing yet';
+
+ @override
+ String get autoDownloadSourceUnnamed => 'Not loaded yet';
+
@override
String get keepNext => 'Keep next';
diff --git a/lib/l10n/app_localizations_fr.dart b/lib/l10n/app_localizations_fr.dart
index 9958d610..c35c039e 100644
--- a/lib/l10n/app_localizations_fr.dart
+++ b/lib/l10n/app_localizations_fr.dart
@@ -1787,6 +1787,15 @@ class AppLocalizationsFr extends AppLocalizations {
String get autoDownloadSubtitle =>
'Enable per series, podcast, playlist or collection from their detail pages';
+ @override
+ String get autoDownloadEnabledFor => 'Turned on for';
+
+ @override
+ String get autoDownloadEnabledForNone => 'Nothing yet';
+
+ @override
+ String get autoDownloadSourceUnnamed => 'Not loaded yet';
+
@override
String get keepNext => 'Keep next';
diff --git a/lib/l10n/app_localizations_it.dart b/lib/l10n/app_localizations_it.dart
index cbc9ebe8..dd98159e 100644
--- a/lib/l10n/app_localizations_it.dart
+++ b/lib/l10n/app_localizations_it.dart
@@ -1775,6 +1775,15 @@ class AppLocalizationsIt extends AppLocalizations {
String get autoDownloadSubtitle =>
'Enable per series, podcast, playlist or collection from their detail pages';
+ @override
+ String get autoDownloadEnabledFor => 'Turned on for';
+
+ @override
+ String get autoDownloadEnabledForNone => 'Nothing yet';
+
+ @override
+ String get autoDownloadSourceUnnamed => 'Not loaded yet';
+
@override
String get keepNext => 'Keep next';
diff --git a/lib/l10n/app_localizations_ja.dart b/lib/l10n/app_localizations_ja.dart
index b917e95f..7a808165 100644
--- a/lib/l10n/app_localizations_ja.dart
+++ b/lib/l10n/app_localizations_ja.dart
@@ -1775,6 +1775,15 @@ class AppLocalizationsJa extends AppLocalizations {
String get autoDownloadSubtitle =>
'Enable per series, podcast, playlist or collection from their detail pages';
+ @override
+ String get autoDownloadEnabledFor => 'Turned on for';
+
+ @override
+ String get autoDownloadEnabledForNone => 'Nothing yet';
+
+ @override
+ String get autoDownloadSourceUnnamed => 'Not loaded yet';
+
@override
String get keepNext => 'Keep next';
diff --git a/lib/l10n/app_localizations_no.dart b/lib/l10n/app_localizations_no.dart
index 2fc284e3..f8bb5118 100644
--- a/lib/l10n/app_localizations_no.dart
+++ b/lib/l10n/app_localizations_no.dart
@@ -1775,6 +1775,15 @@ class AppLocalizationsNo extends AppLocalizations {
String get autoDownloadSubtitle =>
'Enable per series, podcast, playlist or collection from their detail pages';
+ @override
+ String get autoDownloadEnabledFor => 'Turned on for';
+
+ @override
+ String get autoDownloadEnabledForNone => 'Nothing yet';
+
+ @override
+ String get autoDownloadSourceUnnamed => 'Not loaded yet';
+
@override
String get keepNext => 'Keep next';
diff --git a/lib/l10n/app_localizations_pt.dart b/lib/l10n/app_localizations_pt.dart
index 6de7c974..69922660 100644
--- a/lib/l10n/app_localizations_pt.dart
+++ b/lib/l10n/app_localizations_pt.dart
@@ -1775,6 +1775,15 @@ class AppLocalizationsPt extends AppLocalizations {
String get autoDownloadSubtitle =>
'Enable per series, podcast, playlist or collection from their detail pages';
+ @override
+ String get autoDownloadEnabledFor => 'Turned on for';
+
+ @override
+ String get autoDownloadEnabledForNone => 'Nothing yet';
+
+ @override
+ String get autoDownloadSourceUnnamed => 'Not loaded yet';
+
@override
String get keepNext => 'Keep next';
diff --git a/lib/l10n/app_localizations_ro.dart b/lib/l10n/app_localizations_ro.dart
index 69cc519a..93356eb2 100644
--- a/lib/l10n/app_localizations_ro.dart
+++ b/lib/l10n/app_localizations_ro.dart
@@ -1775,6 +1775,15 @@ class AppLocalizationsRo extends AppLocalizations {
String get autoDownloadSubtitle =>
'Enable per series, podcast, playlist or collection from their detail pages';
+ @override
+ String get autoDownloadEnabledFor => 'Turned on for';
+
+ @override
+ String get autoDownloadEnabledForNone => 'Nothing yet';
+
+ @override
+ String get autoDownloadSourceUnnamed => 'Not loaded yet';
+
@override
String get keepNext => 'Keep next';
diff --git a/lib/l10n/app_localizations_ru.dart b/lib/l10n/app_localizations_ru.dart
index 1b957dd3..d421d2ec 100644
--- a/lib/l10n/app_localizations_ru.dart
+++ b/lib/l10n/app_localizations_ru.dart
@@ -1779,6 +1779,15 @@ class AppLocalizationsRu extends AppLocalizations {
String get autoDownloadSubtitle =>
'Enable per series, podcast, playlist or collection from their detail pages';
+ @override
+ String get autoDownloadEnabledFor => 'Turned on for';
+
+ @override
+ String get autoDownloadEnabledForNone => 'Nothing yet';
+
+ @override
+ String get autoDownloadSourceUnnamed => 'Not loaded yet';
+
@override
String get keepNext => 'Keep next';
diff --git a/lib/l10n/app_localizations_zh.dart b/lib/l10n/app_localizations_zh.dart
index 3cd4a000..9666d6ba 100644
--- a/lib/l10n/app_localizations_zh.dart
+++ b/lib/l10n/app_localizations_zh.dart
@@ -1739,6 +1739,15 @@ class AppLocalizationsZh extends AppLocalizations {
@override
String get autoDownloadSubtitle => '在系列或播客详情页单独启用';
+ @override
+ String get autoDownloadEnabledFor => 'Turned on for';
+
+ @override
+ String get autoDownloadEnabledForNone => 'Nothing yet';
+
+ @override
+ String get autoDownloadSourceUnnamed => 'Not loaded yet';
+
@override
String get keepNext => '保留接下来';
diff --git a/lib/providers/_lp_core.dart b/lib/providers/_lp_core.dart
index a6101828..f6c90845 100644
--- a/lib/providers/_lp_core.dart
+++ b/lib/providers/_lp_core.dart
@@ -35,6 +35,71 @@ mixin _CoreMixin on ChangeNotifier, _StateMixin {
}
}
+ /// Everything auto-download is currently turned on for, resolved to a name
+ /// where we can. The stored set is bare ids with no type, so entries we
+ /// can't match against the cached series/playlist/collection/show data are
+ /// still returned unnamed rather than hidden - a toggle you can't see is a
+ /// toggle you can't turn off.
+ List<Map<String, String>> enabledAutoDownloadSources() {
+ final sources = <Map<String, String>>[];
+ for (final id in _rollingDownloadSeries) {
+ String? name;
+ var kind = 'unknown';
+
+ for (final raw in _playlists) {
+ if (raw is Map<String, dynamic> && raw['id'] == id) {
+ name = raw['name'] as String?;
+ kind = 'playlist';
+ break;
+ }
+ }
+ if (name == null) {
+ for (final raw in _collections) {
+ if (raw is Map<String, dynamic> && raw['id'] == id) {
+ name = raw['name'] as String?;
+ kind = 'collection';
+ break;
+ }
+ }
+ }
+ if (name == null) {
+ for (final raw in _series) {
+ if (raw is Map<String, dynamic> && raw['id'] == id) {
+ name = raw['name'] as String?;
+ kind = 'series';
+ break;
+ }
+ }
+ }
+ if (name == null) {
+ for (final cached in _absorbingItemCache.values) {
+ if (cached['id'] != id) continue;
+ final media = cached['media'] as Map<String, dynamic>? ?? const {};
+ final metadata =
+ media['metadata'] as Map<String, dynamic>? ?? const {};
+ name = metadata['title'] as String?;
+ if (name != null) kind = 'podcast';
+ break;
+ }
+ }
+
+ sources.add({
+ 'id': id,
+ 'kind': kind,
+ if (name != null && name.isNotEmpty) 'name': name,
+ });
+ }
+ sources.sort((a, b) {
+ final an = a['name'];
+ final bn = b['name'];
+ if (an == null && bn == null) return a['id']!.compareTo(b['id']!);
+ if (an == null) return 1;
+ if (bn == null) return -1;
+ return an.toLowerCase().compareTo(bn.toLowerCase());
+ });
+ return sources;
+ }
+
Future<void> _loadRollingDownloadSeries() async {
_rollingDownloadSeries =
(await ScopedPrefs.getStringList('rolling_download_series')).toSet();
diff --git a/lib/screens/settings_screen.dart b/lib/screens/settings_screen.dart
index f2546dd2..f126feac 100644
--- a/lib/screens/settings_screen.dart
+++ b/lib/screens/settings_screen.dart
@@ -1124,6 +1124,68 @@ class _SettingsScreenState extends State<SettingsScreen> {
localeNotifier.value = picked.isEmpty ? null : Locale(picked);
}
+ IconData _autoDownloadSourceIcon(String? kind) {
+ switch (kind) {
+ case 'playlist':
+ return Icons.playlist_play_rounded;
+ case 'collection':
+ return Icons.collections_bookmark_rounded;
+ case 'series':
+ return Icons.auto_stories_rounded;
+ case 'podcast':
+ return Icons.podcasts_rounded;
+ default:
+ return Icons.help_outline_rounded;
+ }
+ }
+
+ /// Auto-download is switched on per source from that source's own page, so
+ /// without this there is nowhere to see what it's on for - a list left
+ /// enabled keeps stocking itself with nothing pointing at it.
+ Widget _buildAutoDownloadSources(
+ LibraryProvider lib, AppLocalizations l, ColorScheme cs, TextTheme tt) {
+ final sources = lib.enabledAutoDownloadSources();
+ return Padding(
+ padding: const EdgeInsets.fromLTRB(16, 0, 16, 8),
+ child: Column(
+ crossAxisAlignment: CrossAxisAlignment.start,
+ children: [
+ Text(l.autoDownloadEnabledFor,
+ style: tt.bodyMedium?.copyWith(color: cs.onSurface)),
+ const SizedBox(height: 4),
+ if (sources.isEmpty)
+ Text(l.autoDownloadEnabledForNone,
+ style: tt.bodySmall?.copyWith(color: cs.onSurfaceVariant))
+ else
+ ...sources.map((source) {
+ final name = source['name'];
+ return Row(children: [
+ Icon(_autoDownloadSourceIcon(source['kind']),
+ size: 18, color: cs.onSurfaceVariant),
+ const SizedBox(width: 8),
+ Expanded(
+ child: Text(
+ name ?? l.autoDownloadSourceUnnamed,
+ style: tt.bodySmall?.copyWith(
+ color: name == null ? cs.onSurfaceVariant : cs.onSurface,
+ fontStyle: name == null ? FontStyle.italic : null),
+ maxLines: 1,
+ overflow: TextOverflow.ellipsis,
+ ),
+ ),
+ IconButton(
+ icon: const Icon(Icons.close_rounded, size: 18),
+ tooltip: l.turnAutoDownloadOff,
+ visualDensity: VisualDensity.compact,
+ onPressed: () => lib.disableRollingDownload(source['id']!),
+ ),
+ ]);
+ }),
+ ],
+ ),
+ );
+ }
+
Widget _infoIcon(String title, String content) {
final cs = Theme.of(context).colorScheme;
final l = AppLocalizations.of(context)!;
@@ -2921,6 +2983,7 @@ class _SettingsScreenState extends State<SettingsScreen> {
style: tt.bodySmall?.copyWith(color: cs.onSurfaceVariant)),
leading: Icon(Icons.downloading_rounded, color: cs.primary),
),
+ _buildAutoDownloadSources(lib, l, cs, tt),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Column(
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dart
index 4ae09f1c..a81599e4 100644
--- a/lib/services/audio_player_service.dart
+++ b/lib/services/audio_player_service.dart
@@ -1695,10 +1695,32 @@ class AudioPlayerService extends ChangeNotifier {
final progressKey = next['episodeId'] != null
? '$nextItemId-${next['episodeId']}'
: nextItemId;
+ double localS = 0;
final saved = await _progressSync.getLocal(progressKey);
if (saved != null && !(saved['isFinished'] as bool? ?? false)) {
- startS = (saved['currentTime'] as num?)?.toDouble() ?? 0;
+ localS = (saved['currentTime'] as num?)?.toDouble() ?? 0;
}
+ startS = localS;
+ // Local storage can be empty or behind when the progress lives on the
+ // server (another device, fresh install). Server-ahead wins, same as
+ // the playItem position pick.
+ double serverS = 0;
+ if (_api != null && !_isOfflineMode) {
+ try {
+ final server = await _api!
+ .getItemProgress(progressKey)
+ .timeout(const Duration(seconds: 5), onTimeout: () => null);
+ final serverTime = (server?['currentTime'] as num?)?.toDouble() ?? 0;
+ final serverFinished = server?['isFinished'] as bool? ?? false;
+ if (!serverFinished) serverS = serverTime;
+ if (serverS > startS) startS = serverS;
+ } catch (_) {}
+ }
+ debugPrint(
+ '[PreBuffer] Resume pick for $progressKey: '
+ 'local=${localS.toStringAsFixed(1)}s server=${serverS.toStringAsFixed(1)}s '
+ '-> start=${startS.toStringAsFixed(1)}s',
+ );
next['startS'] = startS;
// Build audio source for the next item.
@@ -1765,7 +1787,8 @@ class AudioPlayerService extends ChangeNotifier {
}
_preloadedNextBook = next;
debugPrint(
- '[PreBuffer] Pre-loaded next item: ${next['title']} ($nextItemId)',
+ '[PreBuffer] Pre-loaded next item: ${next['title']} ($nextItemId) '
+ 'start=${startS.toStringAsFixed(1)}s',
);
if (Platform.isIOS) {
@@ -1848,6 +1871,7 @@ class AudioPlayerService extends ChangeNotifier {
_autoQueueAdvancing = true;
_beginAdvanceBuffering();
debugPrint('[PreBuffer] Auto-queue advanced to ${next['title']}');
+ final oldTrackCount = _currentBookTrackCount;
final oldItemId = _currentItemId;
final oldEpisodeId = _currentEpisodeId;
@@ -1873,6 +1897,12 @@ class AudioPlayerService extends ChangeNotifier {
_playbackSessionId = null;
final startS = (next['startS'] as num?)?.toDouble() ?? 0;
_lastKnownPositionSec = startS;
+ final newKey = _currentEpisodeId != null
+ ? '$_currentItemId-$_currentEpisodeId'
+ : _currentItemId;
+ debugPrint(
+ '[PreBuffer] Promoted $oldKey -> $newKey start=${startS.toStringAsFixed(1)}s',
+ );
_lastNotifiedChapterIndex = -1;
_currentBookTrackCount = 1;
_preloadedNextBook = null;
@@ -1900,6 +1930,8 @@ class AudioPlayerService extends ChangeNotifier {
if (Platform.isIOS) {
unawaited(_iosAutoAdvanceKick());
+ } else {
+ unawaited(_finalizeAndroidAutoAdvance(oldTrackCount, startS));
}
// Notify LibraryProvider via auto-queue-specific callback (skipAutoAdvance
@@ -1916,6 +1948,39 @@ class AudioPlayerService extends ChangeNotifier {
_autoQueueAdvancing = false;
}
+ /// ExoPlayer walks into the pre-buffered book at position 0 with the
+ /// finished item's tracks still in front of it in the concat, so absolute
+ /// seeks map into the dead item (and clamp at its end) and the saved resume
+ /// position is never applied. Drop the old tracks so the book becomes
+ /// index 0, restore single-file offsets, and jump to where the user left it.
+ Future<void> _finalizeAndroidAutoAdvance(
+ int oldTrackCount,
+ double startS,
+ ) async {
+ try {
+ final concat = _activeConcatSource;
+ if (concat != null &&
+ oldTrackCount > 0 &&
+ concat.length > oldTrackCount) {
+ await concat.removeRange(0, oldTrackCount);
+ debugPrint(
+ '[PreBuffer] Dropped $oldTrackCount finished track(s); concat now ${concat.length}',
+ );
+ }
+ _trackStartOffsets = [0.0];
+ _currentTrackIndex = 0;
+ if (startS > 0) {
+ await _seekAbsolute(startS);
+ clearSeekTarget();
+ debugPrint(
+ '[PreBuffer] Resumed advanced book at ${startS.toStringAsFixed(1)}s',
+ );
+ }
+ } catch (e) {
+ debugPrint('[PreBuffer] Android advance finalize failed: $e');
+ }
+ }
+
// Set when native commitAdvance swaps in a foreign AVPlayerItem. just_audio
// sees _index = -1 from then on; we need to rebuild its source the next
// time the app is foregrounded so position and pause work normally.
@@ -2292,6 +2357,11 @@ class AudioPlayerService extends ChangeNotifier {
final trackEnd = _trackStartOffsets[i + 1];
if (absoluteSeconds < trackEnd || i == _trackStartOffsets.length - 2) {
final localOffset = absoluteSeconds - trackStart;
+ debugPrint(
+ '[Player] Seek ${absoluteSeconds.toStringAsFixed(1)}s -> track $i '
+ 'at ${localOffset.toStringAsFixed(1)}s '
+ '(${_trackStartOffsets.length - 1} tracks)',
+ );
// Update index BEFORE seeking so positionStream events use the right offset
_currentTrackIndex = i;
await _player!.seek(
diff --git a/lib/services/progress_sync_service.dart b/lib/services/progress_sync_service.dart
index cb1313a2..e683305e 100644
--- a/lib/services/progress_sync_service.dart
+++ b/lib/services/progress_sync_service.dart
@@ -48,10 +48,17 @@ class ProgressSyncService {
required double speed,
bool? isFinished,
}) async {
+ final existing = await getLocal(itemId);
// Preserve existing isFinished flag if not explicitly provided
- if (isFinished == null) {
- final existing = await getLocal(itemId);
- isFinished = existing?['isFinished'] as bool? ?? false;
+ isFinished ??= existing?['isFinished'] as bool? ?? false;
+ // Tripwire for the progress-loss family: a save that moves an item
+ // backward by minutes is almost always a bug upstream, not listening.
+ final previous = (existing?['currentTime'] as num?)?.toDouble();
+ if (previous != null && previous - currentTime > 300) {
+ debugPrint(
+ '[ProgressDiag] $itemId local save drops '
+ '${previous.toStringAsFixed(1)}s -> ${currentTime.toStringAsFixed(1)}s',
+ );
}
final data = {
'itemId': itemId,