A cross platform Audiobookshelf client for Android and iOS
Dart
Stop two copies of a book playing at the same time on iPhone after starting from the widget
Barnabas7ab3b5d60519
diff --git a/ios/Runner/AbsorbPlayerCore.swift b/ios/Runner/AbsorbPlayerCore.swiftindex 9a2a3003..41698f0d 100644--- a/ios/Runner/AbsorbPlayerCore.swift+++ b/ios/Runner/AbsorbPlayerCore.swift@@ -142,6 +142,28 @@ final class AbsorbPlayerCore: NSObject, AbsorbPlayerCoreProtocol, @unchecked Sen// MARK: - Hand-off+ /// Another process (the foreground app) has claimed audio ownership. Stash our+ /// spot so the owner can resume from it, drop the now-playing flag, and stop+ /// the engine so we don't leave a second stream playing that the foreground+ /// app can't reach. This is the fix for two concurrent streams (#285): a+ /// widget play intent can start playback in a background process, and without+ /// this that stream keeps going after the app is reopened and the user presses+ /// play, leaving two overlapping streams from the same book.+ func yieldToForegroundOwner() {+ queue.async { [weak self] in+ guard let self = self else { return }+ guard AbsorbAudioEngine.shared.isLoaded || AbsorbAudioEngine.shared.isPlaying else {+ self.emit("[NativeCore] yieldToForegroundOwner: engine idle, nothing to stop")+ return+ }+ self.emit("[NativeCore] yieldToForegroundOwner: stopping at \(self.globalPosition())s so the foreground app can take over")+ self.savePosition()+ UserDefaults(suiteName: Self.appGroup)?.set(false, forKey: "widget_is_playing")+ self.stopServerSyncTimer()+ AbsorbAudioEngine.shared.stop()+ }+ }+private func flutterIsAlive() -> Bool {let defaults = UserDefaults(suiteName: Self.appGroup)guard let aliveAt = defaults?.object(forKey: "audio_owner_alive_at") as? Int else {diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swiftindex bf2fd08f..ae3a76af 100644--- a/ios/Runner/AppDelegate.swift+++ b/ios/Runner/AppDelegate.swift@@ -13,6 +13,10 @@ let flutterEngine = FlutterEngine(name: "SharedEngine", project: nil, allowHeadl@objc class AppDelegate: FlutterAppDelegate {private var widgetChannel: FlutterMethodChannel?+ /// True once this process has become the foreground audio owner. Used to+ /// ignore our own takeover broadcast so we don't stop our own playback (#285).+ private var didClaimAudioOwnership = false+override func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?@@ -225,6 +229,7 @@ let flutterEngine = FlutterEngine(name: "SharedEngine", project: nil, allowHeadl"com.barnabas.absorb.widget.playPause","com.barnabas.absorb.widget.skipBack","com.barnabas.absorb.widget.skipForward",+ "com.barnabas.absorb.host.takeover",]for name in names {CFNotificationCenterAddObserver(@@ -234,6 +239,21 @@ let flutterEngine = FlutterEngine(name: "SharedEngine", project: nil, allowHeadllet rawName = name?.rawValue as String? else { return }NSLog("[WidgetDebug] AppDelegate received Darwin notification: %@", rawName)let appDelegate = Unmanaged<AppDelegate>.fromOpaque(observer).takeUnretainedValue()++ // A foreground process claimed audio ownership. If that's not us,+ // stop this process's engine so two streams can't overlap (#285).+ // The owner itself set didClaimAudioOwnership and is .active, so it+ // ignores its own broadcast.+ if rawName == "com.barnabas.absorb.host.takeover" {+ DispatchQueue.main.async {+ if appDelegate.didClaimAudioOwnership { return }+ if UIApplication.shared.applicationState == .active { return }+ NSLog("[WidgetDebug] takeover: foreground app claimed audio, stopping this process's engine")+ AbsorbPlayerCore.shared.yieldToForegroundOwner()+ }+ return+ }+let action: Stringswitch rawName {case "com.barnabas.absorb.widget.playPause": action = "playPause"@@ -264,6 +284,16 @@ let flutterEngine = FlutterEngine(name: "SharedEngine", project: nil, allowHeadlNSLog("[WidgetDebug] AppDelegate registered %d Darwin notification observers", names.count)}+ /// Mark this process as the foreground audio owner and tell any other process+ /// (e.g. one a widget play intent launched in the background) to stop its+ /// engine, so two streams can't overlap (#285). A no-op when this is the only+ /// process. Called from SceneDelegate.sceneDidBecomeActive.+ func claimAudioOwnershipAndNotifyOthers() {+ didClaimAudioOwnership = true+ UserDefaults(suiteName: absorbAppGroup)?.set(Int(getpid()), forKey: "audio_owner_pid")+ postAbsorbDarwinNotification("com.barnabas.absorb.host.takeover")+ }+private func registerPlatformChannels() {let messenger = flutterEngine.binaryMessengerdiff --git a/ios/Runner/SceneDelegate.swift b/ios/Runner/SceneDelegate.swiftindex 42652db2..e6f6494c 100644--- a/ios/Runner/SceneDelegate.swift+++ b/ios/Runner/SceneDelegate.swift@@ -44,6 +44,11 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {pendingShortcut = nilforwardShortcut(shortcut)}++ // This scene is now the foreground process. Claim audio ownership and tell+ // any background process (e.g. one a widget play intent launched) to stop,+ // so two streams can't overlap (#285).+ (UIApplication.shared.delegate as? AppDelegate)?.claimAudioOwnershipAndNotifyOthers()}// Warm-launch via shortcut. iOS 13+ scene-based apps get the shortcut here