Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Instant resume, default streaming cache, buffer tuning, queue labels

Nathan Poulsonc8f5f11cc566
diff --git a/lib/screens/absorbing_screen.dart b/lib/screens/absorbing_screen.dart
index dcad7ebd..5628d51b 100644
--- a/lib/screens/absorbing_screen.dart
+++ b/lib/screens/absorbing_screen.dart
@@ -607,7 +607,9 @@ class _AbsorbingScreenState extends State<AbsorbingScreen> {
if (_queueMode != 'off') ...[
const SizedBox(width: 4),
Text(
- _queueMode == 'auto_next' ? 'Auto' : 'Manual',
+ _queueMode == 'auto_next'
+ ? (_mergeLibraries ? 'Auto' : lib.isPodcastLibrary ? 'Show' : 'Series')
+ : 'Manual',
style: TextStyle(fontSize: 10, fontWeight: FontWeight.w600, color: cs.primary),
),
],
@@ -742,6 +744,8 @@ class _AbsorbingScreenState extends State<AbsorbingScreen> {
lib: lib,
absorbingKeyFn: _absorbingKey,
queueMode: _queueMode,
+ isMerged: _mergeLibraries,
+ isPodcast: lib.isPodcastLibrary,
onQueueModeChanged: (mode) async {
setState(() => _queueMode = mode);
if (_mergeLibraries) {
@@ -828,6 +832,8 @@ class _ReorderAbsorbingSheet extends StatefulWidget {
final LibraryProvider lib;
final String Function(Map<String, dynamic>) absorbingKeyFn;
final String queueMode;
+ final bool isMerged;
+ final bool isPodcast;
final ValueChanged<String> onQueueModeChanged;
const _ReorderAbsorbingSheet({
@@ -836,6 +842,8 @@ class _ReorderAbsorbingSheet extends StatefulWidget {
required this.lib,
required this.absorbingKeyFn,
required this.queueMode,
+ required this.isMerged,
+ required this.isPodcast,
required this.onQueueModeChanged,
});
@@ -898,10 +906,11 @@ class _ReorderAbsorbingSheetState extends State<_ReorderAbsorbingSheet> {
Padding(
padding: const EdgeInsets.fromLTRB(16, 0, 16, 8),
child: SegmentedButton<String>(
- segments: const [
- ButtonSegment(value: 'off', icon: Icon(Icons.stop_rounded, size: 16), label: Text('Off')),
- ButtonSegment(value: 'manual', icon: Icon(Icons.queue_music_rounded, size: 16), label: Text('Manual')),
- ButtonSegment(value: 'auto_next', icon: Icon(Icons.skip_next_rounded, size: 16), label: Text('Auto')),
+ segments: [
+ const ButtonSegment(value: 'off', icon: Icon(Icons.stop_rounded, size: 16), label: Text('Off')),
+ const ButtonSegment(value: 'manual', icon: Icon(Icons.queue_music_rounded, size: 16), label: Text('Manual')),
+ ButtonSegment(value: 'auto_next', icon: const Icon(Icons.skip_next_rounded, size: 16),
+ label: Text(widget.isMerged ? 'Auto' : widget.isPodcast ? 'Show' : 'Series')),
],
selected: {_queueMode},
onSelectionChanged: (v) {
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dart
index b2c7cd15..16469376 100644
--- a/lib/services/audio_player_service.dart
+++ b/lib/services/audio_player_service.dart
@@ -29,9 +29,9 @@ class AudioPlayerHandler extends BaseAudioHandler with SeekHandler {
useProxyForRequestHeaders: false,
audioLoadConfiguration: const AudioLoadConfiguration(
androidLoadControl: AndroidLoadControl(
- bufferForPlaybackDuration: Duration(milliseconds: 500),
- bufferForPlaybackAfterRebufferDuration: Duration(seconds: 2),
- targetBufferBytes: 1 * 1024 * 1024, // 1 MB hard cap
+ bufferForPlaybackDuration: Duration(seconds: 2),
+ bufferForPlaybackAfterRebufferDuration: Duration(seconds: 5),
+ targetBufferBytes: 5 * 1024 * 1024, // 5 MB buffer
),
),
);
@@ -2479,51 +2479,7 @@ class AudioPlayerService extends ChangeNotifier {
// Reset server sync clock so the first sync after resume doesn't
// include pause duration as timeListened
_lastServerSync = DateTime.now();
- // Re-create server session if it was closed (e.g. pause timeout)
- // and check if server progress is ahead (e.g. user listened on another client)
- if (_api != null && _currentItemId != null) {
- final manualOffline = (_prefs ?? await SharedPreferences.getInstance())
- .getBool('manual_offline_mode') ?? false;
- if (manualOffline || _isOfflineMode) {
- debugPrint('[Player] Skipping session re-create on resume (manualOffline=$manualOffline, isOffline=$_isOfflineMode)');
- } else {
- try {
- 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');
- final serverPos = (sessionData['currentTime'] as num?)?.toDouble() ?? 0;
- final localPos = position.inMilliseconds / 1000.0;
- if (serverPos > localPos + 5.0) {
- debugPrint('[Player] Server is ahead on resume: server=${serverPos}s vs local=${localPos}s — seeking');
- await _seekAbsolute(serverPos);
- }
- }
- } else {
- // Session still active — check server progress in case another client advanced
- final pKey = _currentEpisodeId != null
- ? '$_currentItemId-$_currentEpisodeId'
- : _currentItemId!;
- final serverProgress = await _api!.getItemProgress(pKey);
- if (serverProgress != null) {
- final serverPos = (serverProgress['currentTime'] as num?)?.toDouble() ?? 0;
- final localPos = position.inMilliseconds / 1000.0;
- if (serverPos > localPos + 5.0) {
- debugPrint('[Player] Server is ahead on resume: server=${serverPos}s vs local=${localPos}s — seeking');
- await _seekAbsolute(serverPos);
- }
- }
- }
- } catch (e) {
- debugPrint('[Player] Failed to check server progress on resume: $e');
- }
- }
- }
- // Re-activate audio session (needed after stop() releases it)
+ // Re-activate audio session (needed after pause timeout releases it)
try { (await AudioSession.instance).setActive(true); } catch (_) {}
// If the player is idle (source was disposed), we need to fully re-initialize
// playback instead of just calling play() on an empty player.
@@ -2542,9 +2498,13 @@ class AudioPlayerService extends ChangeNotifier {
);
return;
}
+ // Start playback immediately — don't wait for server calls
_player?.play();
_logEvent(PlaybackEventType.play);
_onPlaybackStateChangedCallback?.call(true);
+ // Re-create server session and check progress in the background
+ // so resume is instant instead of waiting for network round-trips
+ _resumeServerSync();
// Restart safety-net save timer (stopped on pause to avoid background wakes)
if (_bgSaveTimer == null || !_bgSaveTimer!.isActive) {
@@ -2569,6 +2529,52 @@ class AudioPlayerService extends ChangeNotifier {
notifyListeners();
}
+ /// Re-create server session and check if server progress is ahead.
+ /// Runs in the background so play() returns instantly.
+ void _resumeServerSync() async {
+ if (_api == null || _currentItemId == null) return;
+ final manualOffline = (_prefs ?? await SharedPreferences.getInstance())
+ .getBool('manual_offline_mode') ?? false;
+ if (manualOffline || _isOfflineMode) {
+ debugPrint('[Player] Skipping session re-create on resume (manualOffline=$manualOffline, isOffline=$_isOfflineMode)');
+ return;
+ }
+ try {
+ 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');
+ final serverPos = (sessionData['currentTime'] as num?)?.toDouble() ?? 0;
+ final localPos = position.inMilliseconds / 1000.0;
+ if (serverPos > localPos + 5.0) {
+ debugPrint('[Player] Server is ahead on resume: server=${serverPos}s vs local=${localPos}s - seeking');
+ await _seekAbsolute(serverPos);
+ }
+ }
+ } else {
+ // Session still active - check server progress in case another client advanced
+ final pKey = _currentEpisodeId != null
+ ? '$_currentItemId-$_currentEpisodeId'
+ : _currentItemId!;
+ final serverProgress = await _api!.getItemProgress(pKey);
+ if (serverProgress != null) {
+ final serverPos = (serverProgress['currentTime'] as num?)?.toDouble() ?? 0;
+ final localPos = position.inMilliseconds / 1000.0;
+ if (serverPos > localPos + 5.0) {
+ debugPrint('[Player] Server is ahead on resume: server=${serverPos}s vs local=${localPos}s - seeking');
+ await _seekAbsolute(serverPos);
+ }
+ }
+ }
+ } catch (e) {
+ debugPrint('[Player] Failed to check server progress on resume: $e');
+ }
+ }
+
Future<void> pause() async {
debugPrint('[Service] pause() called');
_playVerifyTimer?.cancel();
diff --git a/lib/services/player_settings.dart b/lib/services/player_settings.dart
index 3c309c27..da53e4ff 100644
--- a/lib/services/player_settings.dart
+++ b/lib/services/player_settings.dart
@@ -234,7 +234,7 @@ class PlayerSettings {
// ── Streaming cache ──
/// 0 = disabled, > 0 = cache size in MB (LRU eviction)
- static Future<int> getStreamingCacheSizeMb() => _get('streamingCacheSizeMb', 0);
+ static Future<int> getStreamingCacheSizeMb() => _get('streamingCacheSizeMb', 256);
static Future<void> setStreamingCacheSizeMb(int value) async {
debugPrint('[Settings] Streaming cache set to: $value MB');
await _set('streamingCacheSizeMb', value);
diff --git a/lib/widgets/book_detail_sheet.dart b/lib/widgets/book_detail_sheet.dart
index d272e3be..83145686 100644
--- a/lib/widgets/book_detail_sheet.dart
+++ b/lib/widgets/book_detail_sheet.dart
@@ -402,7 +402,10 @@ class _BookDetailSheetContentState extends State<_BookDetailSheetContent> {
return FilledButton.icon(
onPressed: showAbsorbingState
- ? () {}
+ ? () {
+ Navigator.of(context).popUntil((route) => route.isFirst);
+ AppShell.goToAbsorbingGlobal();
+ }
: () {
setState(() => _isAbsorbing = true);
_startAbsorb(