Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Stop Android Auto from resuming downloaded books at an old position

Barnabasbda81a4fc7d8
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dart
index 41622e0c..de4006c1 100644
--- a/lib/services/audio_player_service.dart
+++ b/lib/services/audio_player_service.dart
@@ -2598,15 +2598,19 @@ class AudioPlayerService extends ChangeNotifier {
}
}
- // Phase 1.6: if the iOS native player core was driving while Flutter was
- // dead, it stamped a fresher `np_position_s` to the app group. Pick that
- // up so opening absorb after a widget-tap session shows the right spot.
- if (Platform.isIOS && !forceStartTime) {
- final nativePos = await HomeWidgetService()
- .getNativeNowPlayingPosition(itemId, episodeId);
- if (nativePos != null && nativePos > startTime + 1.0) {
- debugPrint('[Player] Resuming from native widget position: ${nativePos}s (was ${startTime}s)');
- startTime = nativePos;
+ // The widget stash is independent of the scoped SharedPreferences cache.
+ // That matters on Android Auto cold starts, where a long-lived headless
+ // engine can have an older browse entry and an older preferences snapshot.
+ if (!forceStartTime) {
+ final stashedPos = await HomeWidgetService()
+ .getStashedNowPlayingPosition(itemId, episodeId);
+ final newerStashedPos = HomeWidgetService.newerStashedNowPlayingPosition(
+ startTime,
+ stashedPos,
+ );
+ if (newerStashedPos != null) {
+ debugPrint('[Player] Resuming from stashed widget position: ${newerStashedPos}s (was ${startTime}s)');
+ startTime = newerStashedPos;
localTimestampAtStart = DateTime.now().millisecondsSinceEpoch;
}
}
diff --git a/lib/services/home_widget_service.dart b/lib/services/home_widget_service.dart
index dd6a779a..634bdcc3 100644
--- a/lib/services/home_widget_service.dart
+++ b/lib/services/home_widget_service.dart
@@ -152,17 +152,26 @@ class HomeWidgetService {
}
}
- /// Phase 1.6: read the native player core's last position for an item, so
- /// when Flutter wakes up after a widget-driven session it can pick up
- /// where native left off. Returns null if the stashed `np_item_id` doesn't
- /// match (the app group has data for some other item) or if reads fail.
- ///
- /// iOS only - Android doesn't have the native core path.
- Future<double?> getNativeNowPlayingPosition(
+ @visibleForTesting
+ static bool supportsStashedNowPlayingPosition(TargetPlatform platform) =>
+ platform == TargetPlatform.android || platform == TargetPlatform.iOS;
+
+ static double? newerStashedNowPlayingPosition(
+ double startTime,
+ double? stashedPosition,
+ ) => stashedPosition != null && stashedPosition > startTime + 1.0
+ ? stashedPosition
+ : null;
+
+ /// Read the last stashed playback position for an item. On iOS the native
+ /// player may have advanced it while Flutter was dead. On Android it is an
+ /// independent fallback when a headless Android Auto launch has stale
+ /// SharedPreferences or browse-tree state.
+ Future<double?> getStashedNowPlayingPosition(
String itemId,
String? episodeId,
) async {
- if (!Platform.isIOS) return null;
+ if (!supportsStashedNowPlayingPosition(defaultTargetPlatform)) return null;
try {
final stashedItem = await HomeWidget.getWidgetData<String>('np_item_id');
if (stashedItem != itemId) return null;
@@ -173,7 +182,7 @@ class HomeWidgetService {
final pos = await HomeWidget.getWidgetData<double>('np_position_s');
return pos;
} catch (e) {
- debugPrint('[WidgetDebug] getNativeNowPlayingPosition failed: $e');
+ debugPrint('[WidgetDebug] getStashedNowPlayingPosition failed: $e');
return null;
}
}
diff --git a/pubspec.yaml b/pubspec.yaml
index f8a888a1..280f2b4e 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,7 +1,7 @@
name: absorb
description: An Audiobookshelf Client
publish_to: 'none'
-version: 1.9.3+226
+version: 1.9.3+227
environment:
sdk: '>=3.10.0 <4.0.0'