Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Start playback when CarPlay connects too

Barnabas1ca6763e5d97
diff --git a/lib/services/carplay_service.dart b/lib/services/carplay_service.dart
index 1a56128f..ab2e1a4c 100644
--- a/lib/services/carplay_service.dart
+++ b/lib/services/carplay_service.dart
@@ -98,9 +98,35 @@ class CarPlayService {
if (status != ConnectionStatusTypes.connected) return;
if (_connected) return;
_connected = true;
+ unawaited(_maybeAutoplayOnConnect());
_connectAndRender();
}
+ /// GH #371 (CarPlay side): opt-in autoplay when the car connects with
+ /// nothing loaded. Unlike Android Auto there's an explicit connection
+ /// event, so no browse-stamp heuristics - the _connected edge above is the
+ /// once-per-connection gate. A loaded book means the session is alive and
+ /// a resume belongs to the user (or the head unit's own play command), and
+ /// it keeps a deliberate pause from being undone by a reconnect.
+ Future<void> _maybeAutoplayOnConnect() async {
+ try {
+ final player = AudioPlayerService();
+ if (player.hasBook) return;
+ if (!await PlayerSettings.getAutoplayOnCarConnect()) return;
+ // The connect event can arrive during a cold launch in the car - let
+ // init settle before starting audio.
+ await Future.delayed(const Duration(milliseconds: 1500));
+ if (!_connected) return;
+ if (player.hasBook || player.isPlaying) return;
+ final restore = AudioPlayerService.onColdStartPlayRequested;
+ if (restore == null) return;
+ debugPrint('[AutoPlay] CarPlay connected - resuming last played');
+ await restore();
+ } catch (e) {
+ debugPrint('[AutoPlay] CarPlay autoplay failed: $e');
+ }
+ }
+
Future<void> _connectAndRender() async {
if (!_connected) return;
// Make sure data is loaded before rendering so the first template the
diff --git a/lib/services/player_settings.dart b/lib/services/player_settings.dart
index df2af972..b740ce47 100644
--- a/lib/services/player_settings.dart
+++ b/lib/services/player_settings.dart
@@ -748,9 +748,9 @@ class PlayerSettings {
static Future<void> setDuckBriefInterruptions(bool value) =>
_set('duckBriefInterruptions', value, notify: true);
- // Android only (GH #371): start the last played book when Android Auto
- // connects and nothing is loaded yet. The warm case (session alive but
- // paused) is Android Auto's own "automatically resume media" setting; this
+ // GH #371: start the last played book when the car (Android Auto or
+ // CarPlay) connects and nothing is loaded yet. The warm case (session
+ // alive but paused) belongs to the car system's own resume behavior; this
// covers the cold start where no session exists for it to resume.
static Future<bool> getAutoplayOnCarConnect() => _get('autoplayOnCarConnect', false);
static Future<void> setAutoplayOnCarConnect(bool value) =>