Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Don't pause other apps' audio just by opening Absorb

Barnabas9d014309f2d0
diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swift
index 55b270a4..01508c08 100644
--- a/ios/Runner/AppDelegate.swift
+++ b/ios/Runner/AppDelegate.swift
@@ -26,12 +26,16 @@ let flutterEngine = FlutterEngine(name: "SharedEngine", project: nil, allowHeadl
// Now Playing from appearing on scene-based lifecycle apps.
application.beginReceivingRemoteControlEvents()
- // Pre-configure audio session for playback so iOS knows this app
- // plays audio before the Flutter engine finishes initializing.
+ // 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.
let session = AVAudioSession.sharedInstance()
do {
try session.setCategory(.playback, mode: .spokenAudio)
- try session.setActive(true)
} catch {
print("[AppDelegate] Audio session setup failed: \(error)")
}
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dart
index fa937f52..c198f7d2 100644
--- a/lib/services/audio_player_service.dart
+++ b/lib/services/audio_player_service.dart
@@ -2038,8 +2038,15 @@ class AudioPlayerService extends ChangeNotifier {
service._syncToServer(service.position);
}
}
- // Re-activate audio session to get a fresh system token
- try { (await AudioSession.instance).setActive(true); } catch (_) {}
+ // Re-activate audio session to get a fresh system token — but only while
+ // actually playing. Doing it on every foreground grabbed AUDIOFOCUS_GAIN
+ // even when paused/idle, pausing other apps' audio (e.g. Spotify) the
+ // moment Absorb was opened without the user ever pressing play. When not
+ // playing we don't need focus; the MediaSession refresh below doesn't
+ // require it, and play()/startLocalPlayback reacquire focus themselves.
+ if (service.isPlaying) {
+ try { (await AudioSession.instance).setActive(true); } catch (_) {}
+ }
// Re-push playback state so the system re-registers the MediaSession
_handler?.refreshPlaybackState();
// Re-push media item so notification metadata is fresh