Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Flip the widget play button immediately when tapped while the app is asleep

Barnabas98c1c6474dae
diff --git a/ios/AbsorbPlayerCore/Sources/AbsorbPlayerCore/Intents.swift b/ios/AbsorbPlayerCore/Sources/AbsorbPlayerCore/Intents.swift
index 558dd5df..4ee6c7b3 100644
--- a/ios/AbsorbPlayerCore/Sources/AbsorbPlayerCore/Intents.swift
+++ b/ios/AbsorbPlayerCore/Sources/AbsorbPlayerCore/Intents.swift
@@ -48,6 +48,27 @@ public func absorbFlutterAlive(pid: Int) -> Bool {
return ageMs >= 0 && ageMs < 30_000
}
+/// Stamp that audio actuation is happening right now - a widget play intent
+/// fired, or the native engine is playing. The widget trusts a stored
+/// "playing" flag only while some liveness signal is fresh, and the Flutter
+/// heartbeat can lag by many seconds when a native play wakes a suspended
+/// process - without this stamp the play/pause icon didn't flip until the
+/// heartbeat caught up.
+public func absorbStampAudioActivity() {
+ UserDefaults(suiteName: absorbAppGroup)?.set(
+ Int(Date().timeIntervalSince1970 * 1000), forKey: "audio_active_at")
+}
+
+/// True when audio actuation was stamped within the last 150s (the native
+/// core re-stamps at least every 60s while it drives playback).
+public func absorbAudioActivityFresh() -> Bool {
+ guard let at = UserDefaults(suiteName: absorbAppGroup)?
+ .object(forKey: "audio_active_at") as? Int
+ else { return false }
+ let ageMs = Int(Date().timeIntervalSince1970 * 1000) - at
+ return ageMs >= 0 && ageMs < 150_000
+}
+
/// Claim audio ownership for this process unless the current owner's Flutter
/// is verifiably alive in another process. Called from each widget intent's
/// perform(): iOS chose this process to handle a user-initiated audio action,
@@ -142,6 +163,7 @@ public struct AbsorbPlayPauseIntent: AudioPlaybackIntent {
// Whichever side actually actuates writes the real state right after.
let willPlay = core.willPlayAfterToggle()
UserDefaults(suiteName: absorbAppGroup)?.set(willPlay, forKey: "widget_is_playing")
+ if willPlay { absorbStampAudioActivity() }
core.log("[NativeCore] predicted willPlay=\(willPlay)")
activateAbsorbAudioSession()
core.toggle()
diff --git a/ios/NowPlayingWidget/NowPlayingWidget.swift b/ios/NowPlayingWidget/NowPlayingWidget.swift
index cf695184..e1e71782 100644
--- a/ios/NowPlayingWidget/NowPlayingWidget.swift
+++ b/ios/NowPlayingWidget/NowPlayingWidget.swift
@@ -191,11 +191,14 @@ struct NowPlayingProvider: TimelineProvider {
let author = d?.string(forKey: "widget_author") ?? ""
// A stored "playing" can outlive reality (app killed mid-play,
// overnight jetsam) and used to leave the widget's live timers
- // free-running for hours. Trust it only while the owning process's
- // Flutter heartbeat is fresh - it goes stale within about 30 seconds
- // of playback actually stopping.
+ // free-running for hours. Trust it only while something proves audio
+ // is actually live: the owning process's Flutter heartbeat, or the
+ // native side's activity stamp. The stamp is what lets the icon flip
+ // the moment a widget play wakes a suspended app - the heartbeat
+ // lags that by many seconds.
let storedPlaying = d?.bool(forKey: "widget_is_playing") ?? false
- let isPlaying = storedPlaying && absorbFlutterAlive(pid: absorbAudioOwnerPid())
+ let isPlaying = storedPlaying &&
+ (absorbFlutterAlive(pid: absorbAudioOwnerPid()) || absorbAudioActivityFresh())
// The metadata total the app's cards display; the session total
// (np_total_s) can differ from it by minutes and is only a fallback.
let displayTotal = d?.double(forKey: "np_display_total_s") ?? 0
diff --git a/ios/Runner/AbsorbPlayerCore.swift b/ios/Runner/AbsorbPlayerCore.swift
index bc92316f..5644b9c4 100644
--- a/ios/Runner/AbsorbPlayerCore.swift
+++ b/ios/Runner/AbsorbPlayerCore.swift
@@ -364,6 +364,7 @@ final class AbsorbPlayerCore: NSObject, AbsorbPlayerCoreProtocol, @unchecked Sen
private func syncWidgetState() {
let defaults = UserDefaults(suiteName: Self.appGroup)
defaults?.set(AbsorbAudioEngine.shared.isPlaying, forKey: "widget_is_playing")
+ if AbsorbAudioEngine.shared.isPlaying { absorbStampAudioActivity() }
let total = totalDuration()
if total > 0 {
let permille = Int((globalPosition() / total * 1000).rounded())
@@ -399,6 +400,9 @@ final class AbsorbPlayerCore: NSObject, AbsorbPlayerCoreProtocol, @unchecked Sen
/// app on relaunch) see the right position. Best-effort - no retry on
/// failure since we'll try again on the next 60s tick.
private func pushProgressToServer() {
+ // Runs every 60s while the native core drives playback - keeps the
+ // widget's audio-activity signal fresh so it renders as playing.
+ if AbsorbAudioEngine.shared.isPlaying { absorbStampAudioActivity() }
let defaults = UserDefaults(suiteName: Self.appGroup)
guard let itemId = _currentItemId,
let serverUrl = defaults?.string(forKey: "np_server_url"),