A cross platform Audiobookshelf client for Android and iOS
Dart
Let the headset start playback before the app has been opened and played
Barnabas630757d207ed
diff --git a/ios/Runner.xcodeproj/project.pbxproj b/ios/Runner.xcodeproj/project.pbxprojindex 9b76e67b..cd62e0a6 100644--- a/ios/Runner.xcodeproj/project.pbxproj+++ b/ios/Runner.xcodeproj/project.pbxproj@@ -27,6 +27,7 @@BB10020000000000000E0005 /* AudioEQProcessor.m in Sources */ = {isa = PBXBuildFile; fileRef = BB10010000000000000E0005 /* AudioEQProcessor.m */; };BB10020000000000000E0007 /* AbsorbAudioBridge.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB10010000000000000E0007 /* AbsorbAudioBridge.swift */; };BB10020000000000000E0008 /* CarPlayNowPlaying.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB10010000000000000E0008 /* CarPlayNowPlaying.swift */; };+ BB10020000000000000E0009 /* NowPlayingPrimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB10010000000000000E0009 /* NowPlayingPrimer.swift */; };BB10020000000000000E0020 /* AbsorbPlayerCore in Frameworks */ = {isa = PBXBuildFile; productRef = BB10010000000000000E0020 /* AbsorbPlayerCore */; };BB10020000000000000E0021 /* AbsorbPlayerCore in Frameworks */ = {isa = PBXBuildFile; productRef = BB10010000000000000E0021 /* AbsorbPlayerCore */; };/* End PBXBuildFile section */@@ -107,6 +108,7 @@BB10010000000000000E0006 /* AudioEQProcessor.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AudioEQProcessor.h; sourceTree = "<group>"; };BB10010000000000000E0007 /* AbsorbAudioBridge.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AbsorbAudioBridge.swift; sourceTree = "<group>"; };BB10010000000000000E0008 /* CarPlayNowPlaying.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CarPlayNowPlaying.swift; sourceTree = "<group>"; };+ BB10010000000000000E0009 /* NowPlayingPrimer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NowPlayingPrimer.swift; sourceTree = "<group>"; };B7990BE5669E13951B2BAD46 /* Pods-Runner.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"; sourceTree = "<group>"; };DAACD72AEC68ADA3FA213E6C /* Pods_Runner.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Runner.framework; sourceTree = BUILT_PRODUCTS_DIR; };E4A191144C46AC47851AEE49 /* Pods-RunnerTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RunnerTests.release.xcconfig"; path = "Target Support Files/Pods-RunnerTests/Pods-RunnerTests.release.xcconfig"; sourceTree = "<group>"; };@@ -221,6 +223,7 @@BB10010000000000000E0001 /* AbsorbPlayerCore.swift */,BB10010000000000000E0002 /* IOSQueueAdvancer.swift */,BB10010000000000000E0008 /* CarPlayNowPlaying.swift */,+ BB10010000000000000E0009 /* NowPlayingPrimer.swift */,BB10010000000000000E0030 /* Audio */,74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */,);@@ -530,6 +533,7 @@BB10020000000000000E0005 /* AudioEQProcessor.m in Sources */,BB10020000000000000E0007 /* AbsorbAudioBridge.swift in Sources */,BB10020000000000000E0008 /* CarPlayNowPlaying.swift in Sources */,+ BB10020000000000000E0009 /* NowPlayingPrimer.swift in Sources */,);runOnlyForDeploymentPostprocessing = 0;};diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swiftindex f60d55b2..02d5ae9b 100644--- a/ios/Runner/AppDelegate.swift+++ b/ios/Runner/AppDelegate.swift@@ -32,11 +32,11 @@ let flutterEngine = FlutterEngine(name: "SharedEngine", project: nil, allowHeadl// Pre-configure the audio session category for playback so iOS knows this// app plays long-form audio (lock screen / Control Center controls) before- // the Flutter engine finishes initializing. Do NOT activate the session- // here: setActive(true) at launch interrupts other apps' audio (e.g.- // Spotify) the moment Absorb opens, before the user presses play. The- // playback paths (AbsorbAudioEngine / AbsorbPlayerCore / IOSQueueAdvancer)- // activate the session themselves when audio actually starts.+ // the Flutter engine finishes initializing. Activating the session is left+ // to [NowPlayingPrimer], which only does it when no other app is playing -+ // activating unconditionally here would interrupt Spotify the moment+ // Absorb opens. The playback paths (AbsorbAudioEngine / AbsorbPlayerCore /+ // IOSQueueAdvancer) activate it themselves when audio actually starts.let session = AVAudioSession.sharedInstance()do {try session.setCategory(.playback, mode: .spokenAudio)@@ -61,6 +61,18 @@ let flutterEngine = FlutterEngine(name: "SharedEngine", project: nil, allowHeadl}}+ NowPlayingPrimer.logSink = { [weak self] line in+ DispatchQueue.main.async {+ self?.widgetChannel?.invokeMethod("log", arguments: ["msg": line])+ }+ }+ // Take the Now Playing slot for the last-played book, so the headset works+ // before the user has pressed play in the app. Slightly after launch so the+ // audio session plugin has settled and the log channel is listening.+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {+ NowPlayingPrimer.primeAtLaunch()+ }+// Same routing for the EQ tap's format diagnostics, so when a user// reports "EQ on, this book has no sound" we can see the post-decode// PCM format the tap actually received (low-bitrate AAC m4b oftendiff --git a/ios/Runner/NowPlayingPrimer.swift b/ios/Runner/NowPlayingPrimer.swiftnew file mode 100644index 00000000..43edb231--- /dev/null+++ b/ios/Runner/NowPlayingPrimer.swift@@ -0,0 +1,123 @@+import AVFoundation+import MediaPlayer+import UIKit++/// Claims the iOS Now Playing slot at launch, so a headset play button reaches+/// Absorb before anything has been played in this process.+///+/// iOS hands remote control commands to whichever app it considers "now+/// playing", and an app only becomes a candidate once it has an active audio+/// session AND has actually rendered audio. Absorb used to do neither until the+/// user pressed play inside the app, which is why a headset press after a cold+/// launch went nowhere - or worse, to another device on a multipoint headset.+/// Users reported having to pull the phone out, open Absorb and press play+/// before their headphones would work at all.+///+/// Two parts, both needed:+/// - activate the session, which the app deliberately avoided at launch because+/// it interrupts other apps' audio. That only happens when something else is+/// actually playing, so the guard below is what makes it safe rather than+/// skipping it entirely.+/// - render half a second of silence, because an active session that never+/// produces audio does not reliably win the slot.+enum NowPlayingPrimer {+ static var logSink: ((String) -> Void)?++ private static let appGroup = "group.com.barnabas.absorb"++ /// The silent player has to outlive this function or it deallocates before+ /// the blip finishes and the claim never lands.+ private static var blipPlayer: AVPlayer?++ /// A 44-byte WAV header with no samples: enough for iOS to count as playback,+ /// too short to be heard even if the volume guard failed.+ private static let silentWav =+ "data:audio/wav;base64,UklGRiQAAABXQVZFZm10IBAAAAABAAEAQB8AAEAfAAABAAgAZGF0YQAAAAA="++ /// In the car the controls have to reach us even if something else is+ /// playing, so CarPlay overrides the other-audio guard.+ private static func isCarPlayConnected(_ session: AVAudioSession) -> Bool {+ session.currentRoute.outputs.contains { $0.portType == .carAudio }+ }++ /// Publish what the last-played book was and take the slot for it. Reads the+ /// app-group stash the widget already keeps up to date, so this needs neither+ /// the Flutter engine nor the network - it runs before either is ready.+ static func primeAtLaunch() {+ let session = AVAudioSession.sharedInstance()++ // Someone else is playing. Claiming here would interrupt them the moment+ // Absorb opens, which is exactly what the user did not ask for. CarPlay is+ // the exception: in the car the controls have to reach us regardless.+ let otherAudio = session.isOtherAudioPlaying+ let shouldSilence = session.secondaryAudioShouldBeSilencedHint+ if (otherAudio || shouldSilence), !isCarPlayConnected(session) {+ logSink?(+ "[NowPlayingPrimer] skipped, other audio is playing "+ + "(isOtherAudioPlaying=\(otherAudio) silenceHint=\(shouldSilence))")+ return+ }++ let defaults = UserDefaults(suiteName: appGroup)+ guard let itemId = defaults?.string(forKey: "np_item_id"), !itemId.isEmpty else {+ logSink?("[NowPlayingPrimer] skipped, nothing has been played on this device yet")+ return+ }++ // Launched by the widget to start playing, so audio is already on its way.+ // Priming now would stamp a paused, stale state over the real one.+ if defaults?.bool(forKey: "widget_is_playing") == true {+ logSink?("[NowPlayingPrimer] skipped, playback is already running")+ return+ }++ do {+ try session.setCategory(.playback, mode: .spokenAudio, policy: .longFormAudio)+ try session.setActive(true)+ } catch {+ logSink?("[NowPlayingPrimer] session activate failed: \(error.localizedDescription)")+ return+ }++ let title = defaults?.string(forKey: "np_title")+ ?? defaults?.string(forKey: "widget_title") ?? ""+ let author = defaults?.string(forKey: "np_author")+ ?? defaults?.string(forKey: "widget_author") ?? ""+ let coverPath = defaults?.string(forKey: "np_cover_path")+ ?? defaults?.string(forKey: "widget_cover_path")+ let elapsed = defaults?.double(forKey: "np_position_s") ?? 0+ let duration = defaults?.double(forKey: "np_total_s") ?? 0++ var info: [String: Any] = [+ MPMediaItemPropertyTitle: title,+ MPMediaItemPropertyArtist: author,+ MPNowPlayingInfoPropertyElapsedPlaybackTime: elapsed,+ // Paused: nothing is playing yet, we are only claiming the controls.+ MPNowPlayingInfoPropertyPlaybackRate: 0.0,+ MPNowPlayingInfoPropertyDefaultPlaybackRate: 1.0,+ MPNowPlayingInfoPropertyMediaType: MPNowPlayingInfoMediaType.audio.rawValue,+ MPMediaItemPropertyMediaType: MPMediaType.audioBook.rawValue,+ ]+ if duration > 0 {+ info[MPMediaItemPropertyPlaybackDuration] = duration+ }+ if let coverPath, let img = UIImage(contentsOfFile: coverPath) {+ info[MPMediaItemPropertyArtwork] = MPMediaItemArtwork(boundsSize: img.size) { _ in img }+ }+ MPNowPlayingInfoCenter.default().nowPlayingInfo = info++ guard let url = URL(string: silentWav) else { return }+ let player = AVPlayer(url: url)+ player.allowsExternalPlayback = false+ player.volume = 0+ blipPlayer = player+ player.play()+ DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {+ blipPlayer = nil+ }++ logSink?(+ "[NowPlayingPrimer] claimed Now Playing for \"\(title)\" at "+ + "\(Int(elapsed))s (paused)")+ }+}