Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Fill in the lock screen player after a headset press relaunches the app

Barnabasf5b1e2be5fdf
diff --git a/lib/main.dart b/lib/main.dart
index 9ab3ccb3..8b5f54a4 100644
--- a/lib/main.dart
+++ b/lib/main.dart
@@ -782,6 +782,10 @@ class _AuthGateState extends State<AuthGate> {
// circular import between AudioPlayerService and HomeWidgetService.
AudioPlayerService.onColdStartPlayRequested =
HomeWidgetService().resumeLastPlayedIfAvailable;
+ // A headset press may have cold-launched this process into the background
+ // with the native core already playing - adopt that audio now rather than
+ // leaving the lock screen blank until the next press.
+ unawaited(AudioPlayerService().adoptBackgroundEngineIfRunning());
debugPrint('[Init] AudioPlayerService done (${sw.elapsedMilliseconds}ms)');
try {
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dart
index b2bdd9cb..c3d62bea 100644
--- a/lib/services/audio_player_service.dart
+++ b/lib/services/audio_player_service.dart
@@ -2159,6 +2159,37 @@ class AudioPlayerService extends ChangeNotifier {
}
}
+ /// iOS: a headset press can cold-launch the app into the background with
+ /// the native core already driving the shared engine (its remote commands
+ /// arm at app launch). Dart then boots with no book loaded, so
+ /// audio_service publishes a blank Now Playing over the native core's and
+ /// every later press defers to a player that knows nothing - the lock
+ /// screen shows audio running with no title, cover or progress. Adopt
+ /// instead: restore the last-played item at the engine's own live position,
+ /// which re-publishes full metadata and puts Dart in charge of the audio
+ /// that is already running. Called once from main.dart after init.
+ Future<void> adoptBackgroundEngineIfRunning() async {
+ if (!Platform.isIOS) return;
+ if (_currentItemId != null) return;
+ final player = _player;
+ if (player == null) return;
+ try {
+ final state = await player.engineState();
+ if (state == null || !state.isLoaded || !state.isPlaying) return;
+ debugPrint(
+ '[Player] boot: native engine already playing at '
+ '${state.globalPositionS.toStringAsFixed(1)}s with no book loaded - adopting',
+ );
+ if (state.globalPositionS > 0) {
+ await HomeWidgetService().stashLivePosition(state.globalPositionS);
+ }
+ final restore = AudioPlayerService.onColdStartPlayRequested;
+ if (restore != null) await restore();
+ } catch (e) {
+ debugPrint('[Player] boot engine adopt failed: $e');
+ }
+ }
+
/// iOS foreground reconciliation after a possible widget-driven session.
///
/// The shared engine (AbsorbAudioEngine) persists across Flutter suspension,
diff --git a/lib/services/home_widget_service.dart b/lib/services/home_widget_service.dart
index 10fd9e8a..54fbbde6 100644
--- a/lib/services/home_widget_service.dart
+++ b/lib/services/home_widget_service.dart
@@ -177,6 +177,17 @@ class HomeWidgetService {
? stashedPosition
: null;
+ /// Overwrite the stashed position with a live engine reading, so a restore
+ /// that prefers the stash resumes exactly where the audio actually is
+ /// instead of jumping back to an older save.
+ Future<void> stashLivePosition(double seconds) async {
+ try {
+ await HomeWidget.saveWidgetData<double>('np_position_s', seconds);
+ } catch (e) {
+ debugPrint('[WidgetDebug] stashLivePosition failed: $e');
+ }
+ }
+
/// 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