A cross platform Audiobookshelf client for Android and iOS
Dart
Hopefully take the headset controls back when you come back from another app
Barnabasa127553b20ab
diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swiftindex 703c024a..e75fb696 100644--- a/ios/Runner/AppDelegate.swift+++ b/ios/Runner/AppDelegate.swift@@ -620,6 +620,14 @@ let flutterEngine = FlutterEngine(name: "SharedEngine", project: nil, allowHeadl]result(info)+ case "reclaimNowPlaying":+ // Something else may have taken the Now Playing slot while Absorb sat+ // paused. Activating the session and republishing metadata is not+ // enough to get it back - only rendered audio moves the slot - so the+ // reassert goes through the primer rather than doing it in Dart.+ let reclaimReason = args?["reason"] as? String ?? "unknown"+ result(NowPlayingPrimer.reclaim(reason: reclaimReason))+case "primeNowPlaying":let title = args?["title"] as? String ?? ""let artist = args?["artist"] as? String ?? ""diff --git a/ios/Runner/NowPlayingPrimer.swift b/ios/Runner/NowPlayingPrimer.swiftindex dd0da45d..59c22d19 100644--- a/ios/Runner/NowPlayingPrimer.swift+++ b/ios/Runner/NowPlayingPrimer.swift@@ -2,8 +2,8 @@ import AVFoundationimport MediaPlayerimport UIKit-/// Claims the iOS Now Playing slot at launch, so a headset play button reaches-/// Absorb before anything has been played in this process.+/// Claims the iOS Now Playing slot for Absorb, so a headset play button reaches+/// this app rather than whatever played last.////// 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@@ -19,7 +19,10 @@ import UIKit/// 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.+/// produces audio does not reliably win the slot. Build 249 activated and+/// published metadata without rendering, and lost the slot twice in the+/// field; 251 added real samples and held it. Anything that wants the slot+/// back has to come through here for that reason.enum NowPlayingPrimer {static var logSink: ((String) -> Void)?@@ -83,12 +86,39 @@ enum NowPlayingPrimer {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.+ /// Launch: 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() {+ _ = claim(reason: "launch", publishFromStash: true)+ }++ /// Take the slot back mid-session, after something else may have won it: the+ /// user opened another app that played audio, an interruption came and went+ /// while Absorb sat suspended, or the headphones were stowed. Metadata is+ /// already live in audio_service by this point, so nothing is republished+ /// here - only the activation and the blip, which is the part that actually+ /// moves the slot. Returns whether the blip started; the "blip finished+ /// (rendered=)" line is the ground truth for whether it landed.+ @discardableResult+ static func reclaim(reason: String) -> Bool {+ return claim(reason: reason, publishFromStash: false)+ }++ @discardableResult+ private static func claim(reason: String, publishFromStash: Bool) -> Bool {let session = AVAudioSession.sharedInstance()+ // A blip already on its way is the same claim. Starting a second one+ // replaces the player and deallocates the first mid-render, so neither+ // lands - and at launch the scene-activate prime and the first foreground+ // reassert can arrive within a few hundred milliseconds of each other.+ if blipPlayer != nil {+ logSink?("[NowPlayingPrimer] \(reason): skipped, a claim is already in flight")+ return false+ }+// 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.@@ -96,58 +126,70 @@ enum NowPlayingPrimer {let shouldSilence = session.secondaryAudioShouldBeSilencedHintif (otherAudio || shouldSilence), !isCarPlayConnected(session) {logSink?(- "[NowPlayingPrimer] skipped, other audio is playing "+ "[NowPlayingPrimer] \(reason): skipped, other audio is playing "+ "(isOtherAudioPlaying=\(otherAudio) silenceHint=\(shouldSilence))")- return+ return false}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- }+ var title = ""+ var elapsed: Double = 0- // 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+ if publishFromStash {+ guard let itemId = defaults?.string(forKey: "np_item_id"), !itemId.isEmpty else {+ logSink?("[NowPlayingPrimer] skipped, nothing has been played on this device yet")+ return false+ }++ // 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 false+ }}do {- try session.setCategory(.playback, mode: .spokenAudio, policy: .longFormAudio)+ // Re-setting a category that already matches makes iOS emit a route+ // change, which is log noise on every foreground reassert.+ if session.category != .playback {+ try session.setCategory(.playback, mode: .spokenAudio, policy: .longFormAudio)+ }try session.setActive(true)} catch {- logSink?("[NowPlayingPrimer] session activate failed: \(error.localizedDescription)")- return+ logSink?(+ "[NowPlayingPrimer] \(reason): session activate failed: \(error.localizedDescription)")+ return false}- 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 }+ if publishFromStash {+ 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")+ 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}- MPNowPlayingInfoCenter.default().nowPlayingInfo = infodo {let player = try AVAudioPlayer(data: makeSilentWav(durationMs: 600))@@ -155,18 +197,20 @@ enum NowPlayingPrimer {player.delegate = blipDelegateblipPlayer = playerlet started = player.play()- logSink?(- "[NowPlayingPrimer] claimed Now Playing for \"\(title)\" at "- + "\(Int(elapsed))s (paused, blip started=\(started))")- } catch {- logSink?("[NowPlayingPrimer] blip init failed: \(error.localizedDescription)")- return- }- DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {- if blipPlayer != nil {- logSink?("[NowPlayingPrimer] blip never finished - releasing")- blipPlayer = nil+ let opening = publishFromStash+ ? "claimed Now Playing for \"\(title)\" at \(Int(elapsed))s (paused"+ : "reclaimed Now Playing (\(reason)"+ logSink?("[NowPlayingPrimer] \(opening), blip started=\(started))")+ DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {+ if blipPlayer != nil {+ logSink?("[NowPlayingPrimer] blip never finished - releasing")+ blipPlayer = nil+ }}+ return started+ } catch {+ logSink?("[NowPlayingPrimer] \(reason): blip init failed: \(error.localizedDescription)")+ return false}}}diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dartindex 6eee5a0c..078982bc 100644--- a/lib/services/audio_player_service.dart+++ b/lib/services/audio_player_service.dart@@ -2161,14 +2161,22 @@ class AudioPlayerService extends ChangeNotifier {/// iOS: re-take the Now Playing claim after something may have knocked it/// out while paused - an interruption (call, Siri, nav, another app's- /// session), or a stretch suspended in the background where the- /// interruption came and went unseen. iOS deactivates the session for the- /// interrupter, and a paused app that never re-activates quietly falls out- /// of Now Playing candidacy: the next headset press then goes to Apple- /// Music instead. Re-activation happens only when nothing else is audibly- /// playing (the primer's politeness guard) - the claim matters exactly- /// when the next press should reach this app, and grabbing it out from- /// under an app mid-playback would be rude.+ /// session), a stretch suspended in the background where the interruption+ /// came and went unseen, or simply the user opening another app that played+ /// something. iOS deactivates the session for the interrupter, and a paused+ /// app that never re-activates quietly falls out of Now Playing candidacy:+ /// the next headset press then goes to Apple Music instead.+ ///+ /// Re-activating and republishing metadata is NOT enough on its own. iOS+ /// only hands the slot to an app that has actually rendered audio, which is+ /// what build 249 got wrong and 251 fixed with a silent blip - so the claim+ /// goes through the native primer rather than being done here. Doing it in+ /// Dart looked like it worked, because setActive succeeds either way.+ ///+ /// Only runs when nothing else is audibly playing (the primer's politeness+ /// guard, checked on both sides): the claim matters exactly when the next+ /// press should reach this app, and grabbing it out from under an app+ /// mid-playback would be rude.Future<void> reassertIosClaimWhilePaused(String reason) async {if (!Platform.isIOS || !hasBook || isPlaying) return;try {@@ -2182,9 +2190,15 @@ class AudioPlayerService extends ChangeNotifier {return;}await (await AudioSession.instance).setActive(true);+ // Publish this book before the blip, so the slot we take back shows the+ // right title rather than whatever the app that stole it left behind._handler?.refreshPlaybackState();+ final started = await _eqChannelForDiag.invokeMethod<bool>(+ 'reclaimNowPlaying',+ {'reason': reason},+ );debugPrint(- '[AudioSession] re-asserted Now Playing claim while paused ($reason)',+ '[AudioSession] claim reassert ($reason): blip started=$started',);} catch (e) {debugPrint('[AudioSession] claim reassert failed ($reason): $e');