Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Coordinate Flutter and native iOS player via heartbeat

Barnabas018dcd03b233
diff --git a/ios/Runner/AbsorbPlayerCore.swift b/ios/Runner/AbsorbPlayerCore.swift
index a7860036..9eab4924 100644
--- a/ios/Runner/AbsorbPlayerCore.swift
+++ b/ios/Runner/AbsorbPlayerCore.swift
@@ -118,9 +118,27 @@ final class AbsorbPlayerCore: NSObject, AbsorbPlayerCoreProtocol, AVAudioPlayerD
// MARK: - State plumbing
+ /// Phase 1.4 hand-off check. If Flutter wrote a recent
+ /// `audio_owner_alive_at` timestamp, it's alive and owns playback. The
+ /// native core bails so we don't double-drive the audio engine. Stale or
+ /// missing timestamp means Flutter is dead and we can take over.
+ private func flutterIsAlive() -> Bool {
+ let defaults = UserDefaults(suiteName: Self.appGroup)
+ guard let aliveAt = defaults?.object(forKey: "audio_owner_alive_at") as? Int else {
+ return false
+ }
+ let nowMs = Int(Date().timeIntervalSince1970 * 1000)
+ let ageMs = nowMs - aliveAt
+ return ageMs >= 0 && ageMs < 30_000
+ }
+
/// Lazily load the AVAudioPlayer from the app group's stashed playback
/// state. Caches the loaded player; rebuilds if the active item changed.
private func ensureLoaded() {
+ if flutterIsAlive() {
+ emit("[NativeCore] ensureLoaded: Flutter is alive, skipping native load")
+ return
+ }
let defaults = UserDefaults(suiteName: Self.appGroup)
guard let itemId = defaults?.string(forKey: "np_item_id") else {
emit("[NativeCore] ensureLoaded: no np_item_id in app group")
diff --git a/lib/services/home_widget_service.dart b/lib/services/home_widget_service.dart
index e8c750b0..2ea5a792 100644
--- a/lib/services/home_widget_service.dart
+++ b/lib/services/home_widget_service.dart
@@ -28,6 +28,7 @@ class HomeWidgetService {
Timer? _progressTimer;
Timer? _statsTimer;
+ Timer? _heartbeatTimer;
Timer? _pendingUpdate;
String? _lastCoverItemId;
DateTime? _lastUpdate;
@@ -114,11 +115,36 @@ class HomeWidgetService {
// the user to open the app. 15-min cadence matches the refresh throttle.
_statsTimer?.cancel();
_statsTimer = Timer.periodic(_statsThrottle, (_) => refreshStats());
+
+ // Phase 1.4 hand-off heartbeat. The iOS native player core checks this
+ // before driving audio: a recent timestamp means Flutter is alive and
+ // owns playback, so the native side bails. Stale or missing means
+ // Flutter is dead and the widget can take over.
+ if (Platform.isIOS) {
+ _writeOwnerHeartbeat();
+ _heartbeatTimer?.cancel();
+ _heartbeatTimer = Timer.periodic(
+ const Duration(seconds: 10),
+ (_) => _writeOwnerHeartbeat(),
+ );
+ }
+ }
+
+ Future<void> _writeOwnerHeartbeat() async {
+ try {
+ await HomeWidget.saveWidgetData<int>(
+ 'audio_owner_alive_at',
+ DateTime.now().millisecondsSinceEpoch,
+ );
+ } catch (e) {
+ debugPrint('[WidgetDebug] heartbeat write failed: $e');
+ }
}
void dispose() {
_progressTimer?.cancel();
_statsTimer?.cancel();
+ _heartbeatTimer?.cancel();
_pendingUpdate?.cancel();
_clickSub?.cancel();
AudioPlayerService().removeListener(_onPlayerChanged);