Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Stop another app taking over when you resume iOS playback after a pause

Barnabas9699e1fe7f45
diff --git a/ios/Runner/AbsorbPlayerCore.swift b/ios/Runner/AbsorbPlayerCore.swift
index f98b1fb3..f99eafaa 100644
--- a/ios/Runner/AbsorbPlayerCore.swift
+++ b/ios/Runner/AbsorbPlayerCore.swift
@@ -515,6 +515,19 @@ final class AbsorbPlayerCore: NSObject, AbsorbPlayerCoreProtocol, @unchecked Sen
// MARK: - MPRemoteCommandCenter
+ /// Register the remote command handlers up front, without loading a player or
+ /// starting playback. They defer to Flutter while it's alive, so this is a
+ /// no-op for normal foreground use - but it guarantees a native target stays
+ /// registered for the play command. Without it, the handlers were only wired
+ /// the first time the native core played, so a play after iOS suspended a
+ /// paused Flutter found no responsive Now Playing app and iOS handed control
+ /// to Apple Music. Idempotent.
+ func armRemoteCommands() {
+ queue.async { [weak self] in
+ self?.configureRemoteCommandsIfNeeded()
+ }
+ }
+
private func configureRemoteCommandsIfNeeded() {
if _commandsConfigured { return }
_commandsConfigured = true
diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swift
index 8f447fbb..c46c8c0f 100644
--- a/ios/Runner/AppDelegate.swift
+++ b/ios/Runner/AppDelegate.swift
@@ -106,6 +106,17 @@ let flutterEngine = FlutterEngine(name: "SharedEngine", project: nil, allowHeadl
AbsorbPlayerCore.logSink?("[NativeCore] Registered as AppIntent dependency")
}
+ // When the app backgrounds, wire the native core's lock-screen / headphone
+ // command handlers. They defer to Flutter while it's alive, but staying
+ // registered means a play command after iOS suspends a paused Flutter still
+ // has a native target - so Absorb resumes instead of iOS handing the play to
+ // Apple Music. We arm on background (not launch) because Flutter is
+ // guaranteed alive here, and an app must background before iOS suspends it.
+ NotificationCenter.default.addObserver(
+ forName: UIApplication.didEnterBackgroundNotification,
+ object: nil, queue: .main
+ ) { _ in AbsorbPlayerCore.shared.armRemoteCommands() }
+
registerAudioSessionObservers()
return super.application(application, didFinishLaunchingWithOptions: launchOptions)