Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Stash playback state in app group for native player

Barnabasdbb1ee3a151b
diff --git a/lib/services/home_widget_service.dart b/lib/services/home_widget_service.dart
index 3b5f5db5..e8c750b0 100644
--- a/lib/services/home_widget_service.dart
+++ b/lib/services/home_widget_service.dart
@@ -344,7 +344,13 @@ class HomeWidgetService {
await prefs.remove('widget_episode_id');
}
- // Cover art — fire-and-forget so it doesn't block the update.
+ // Phase 1.1: stash the playback state the iOS native player core needs
+ // to resume audio without going through Flutter. AbsorbPlayerCore reads
+ // these from the app group on widget tap. Keys are `np_*` (Now Playing)
+ // to keep them separate from `widget_*` UI fields.
+ await _stashPlaybackStateForNativeCore(player);
+
+ // Cover art - fire-and-forget so it doesn't block the update.
_updateCoverArt(player.currentItemId!);
if (player.isPlaying) {
@@ -362,6 +368,45 @@ class HomeWidgetService {
await _updateAllWidgets();
}
+ /// Phase 1.1: write everything the iOS native player core needs to resume
+ /// playback without Flutter being alive. Lives in the same app group so
+ /// `AbsorbPlayerCore` (Swift) can read it via `UserDefaults(suiteName:)`.
+ ///
+ /// Streaming case (Phase 2) will need the server URL + auth token too;
+ /// for now we only stash the file paths so Phase 1.3 (downloaded books)
+ /// can play.
+ Future<void> _stashPlaybackStateForNativeCore(AudioPlayerService player) async {
+ final itemId = player.currentItemId;
+ if (itemId == null) return;
+ await HomeWidget.saveWidgetData<String>('np_item_id', itemId);
+ await HomeWidget.saveWidgetData<String?>('np_episode_id', player.currentEpisodeId);
+
+ final posSec = player.position.inMilliseconds / 1000.0;
+ await HomeWidget.saveWidgetData<double>('np_position_s', posSec);
+ await HomeWidget.saveWidgetData<double>('np_total_s', player.totalDuration);
+ await HomeWidget.saveWidgetData<double>('np_speed', player.speed);
+
+ // Chapters serialize as a JSON array of {start, end, title} maps. The
+ // native side decodes lazily; if decoding fails we fall back to no
+ // chapter info and play the file straight through.
+ try {
+ await HomeWidget.saveWidgetData<String>(
+ 'np_chapters_json', jsonEncode(player.chapters));
+ } catch (e) {
+ debugPrint('[WidgetDebug] chapter encode failed: $e');
+ await HomeWidget.saveWidgetData<String>('np_chapters_json', '[]');
+ }
+
+ // Downloaded audio file paths (one per track for multi-file books).
+ // Empty list => streaming-only, native core will skip Phase 1 playback
+ // for now and Phase 2 will pick up streaming via auth token.
+ final download = DownloadService().getInfo(itemId);
+ final paths = download.localPaths;
+ await HomeWidget.saveWidgetData<String>(
+ 'np_audio_paths_json', jsonEncode(paths));
+ await HomeWidget.saveWidgetData<bool>('np_is_downloaded', paths.isNotEmpty);
+ }
+
Future<void> _updateAllWidgets() async {
if (Platform.isAndroid) {
await HomeWidget.updateWidget(name: _androidWidgetName);