Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Fix iOS background queue, attempt 10 - native queue handler with rate kick

Barnabas0cc0370d128f
diff --git a/ios/Runner.xcodeproj/project.pbxproj b/ios/Runner.xcodeproj/project.pbxproj
index 17a11886..064bd2c9 100644
--- a/ios/Runner.xcodeproj/project.pbxproj
+++ b/ios/Runner.xcodeproj/project.pbxproj
@@ -21,6 +21,7 @@
AB10020000000000000E0002 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = AB10010000000000000E0002 /* Assets.xcassets */; };
AB10020000000000000E0003 /* NowPlayingWidgetExtension.appex in Embed App Extensions */ = {isa = PBXBuildFile; fileRef = AB10010000000000000E0004 /* NowPlayingWidgetExtension.appex */; settings = {ATTRIBUTES = (RemoveHeadersOnCopy, ); }; };
BB10020000000000000E0001 /* AbsorbPlayerCore.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB10010000000000000E0001 /* AbsorbPlayerCore.swift */; };
+ BB10020000000000000E0002 /* IOSQueueAdvancer.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB10010000000000000E0002 /* IOSQueueAdvancer.swift */; };
BB10020000000000000E0020 /* AbsorbPlayerCore in Frameworks */ = {isa = PBXBuildFile; productRef = BB10010000000000000E0020 /* AbsorbPlayerCore */; };
BB10020000000000000E0021 /* AbsorbPlayerCore in Frameworks */ = {isa = PBXBuildFile; productRef = BB10010000000000000E0021 /* AbsorbPlayerCore */; };
/* End PBXBuildFile section */
@@ -94,6 +95,7 @@
AB10010000000000000E0005 /* Runner.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = Runner.entitlements; sourceTree = "<group>"; };
AB10010000000000000E0006 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
BB10010000000000000E0001 /* AbsorbPlayerCore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AbsorbPlayerCore.swift; sourceTree = "<group>"; };
+ BB10010000000000000E0002 /* IOSQueueAdvancer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IOSQueueAdvancer.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>"; };
@@ -206,6 +208,7 @@
74858FAE1ED2DC5600515810 /* AppDelegate.swift */,
7884E8672EC3CC0400C636F2 /* SceneDelegate.swift */,
BB10010000000000000E0001 /* AbsorbPlayerCore.swift */,
+ BB10010000000000000E0002 /* IOSQueueAdvancer.swift */,
74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */,
);
path = Runner;
@@ -496,6 +499,7 @@
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */,
7884E8682EC3CC0700C636F2 /* SceneDelegate.swift in Sources */,
BB10020000000000000E0001 /* AbsorbPlayerCore.swift in Sources */,
+ BB10020000000000000E0002 /* IOSQueueAdvancer.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swift
index 898bdb50..ef8f42b8 100644
--- a/ios/Runner/AppDelegate.swift
+++ b/ios/Runner/AppDelegate.swift
@@ -63,6 +63,12 @@ let flutterEngine = FlutterEngine(name: "SharedEngine", project: nil, allowHeadl
}
}
+ IOSQueueAdvancer.logSink = { [weak self] line in
+ DispatchQueue.main.async {
+ self?.widgetChannel?.invokeMethod("log", arguments: ["msg": line])
+ }
+ }
+
// Register the native player core as an AppIntent dependency. The widget
// intent declares `@Dependency var core: AbsorbPlayerCoreProtocol` - that
// signals to iOS to launch this host app process to run the intent's
@@ -229,6 +235,8 @@ let flutterEngine = FlutterEngine(name: "SharedEngine", project: nil, allowHeadl
private func registerPlatformChannels() {
let messenger = flutterEngine.binaryMessenger
+ IOSQueueAdvancer.shared.register(with: messenger)
+
// iOS audio output device switching is not implemented yet — iOS routes
// through the system's MPVolumeView/AVRoutePicker rather than letting apps
// pick output devices directly. Stub these so the channel responds.
diff --git a/ios/Runner/IOSQueueAdvancer.swift b/ios/Runner/IOSQueueAdvancer.swift
new file mode 100644
index 00000000..7397c0c1
--- /dev/null
+++ b/ios/Runner/IOSQueueAdvancer.swift
@@ -0,0 +1,182 @@
+import AVFoundation
+import Flutter
+import Foundation
+import MediaPlayer
+import UIKit
+
+/// Handles the cross-book audio transition when queue mode advances on iOS.
+///
+/// AVQueuePlayer auto-advance can leave the player rate at >0 with currentTime
+/// advancing but no audio reaching the output route when the screen is locked.
+/// This class forces the engine to restart on its own AVQueuePlayer (the one
+/// just_audio created) by pausing and re-applying the rate, and updates the
+/// lock screen Now Playing info from native code in the same moment so iOS
+/// keeps showing controls during the swap.
+final class IOSQueueAdvancer: NSObject {
+ static let shared = IOSQueueAdvancer()
+
+ static var logSink: ((String) -> Void)?
+
+ private let queue = DispatchQueue(label: "com.barnabas.absorb.queueadvancer")
+
+ private weak var _justAudioPlayer: AVQueuePlayer?
+ private var _justAudioPlayerId: String?
+
+ private var _nextTitle: String = ""
+ private var _nextArtist: String = ""
+ private var _nextDurationS: Double = 0
+ private var _nextCoverPath: String?
+
+ private override init() {
+ super.init()
+ NotificationCenter.default.addObserver(
+ forName: Notification.Name("AbsorbJustAudioPlayerReady"),
+ object: nil,
+ queue: .main
+ ) { [weak self] note in
+ guard let player = note.userInfo?["player"] as? AVQueuePlayer else { return }
+ let playerId = note.userInfo?["playerId"] as? String
+ self?.queue.async {
+ self?._justAudioPlayer = player
+ self?._justAudioPlayerId = playerId
+ self?.emit("[QueueAdvancer] captured just_audio player id=\(playerId ?? "?")")
+ }
+ }
+ NotificationCenter.default.addObserver(
+ forName: Notification.Name("AbsorbJustAudioPlayerReleased"),
+ object: nil,
+ queue: .main
+ ) { [weak self] note in
+ let playerId = note.userInfo?["playerId"] as? String
+ self?.queue.async {
+ if self?._justAudioPlayerId == playerId {
+ self?._justAudioPlayer = nil
+ self?._justAudioPlayerId = nil
+ self?.emit("[QueueAdvancer] released just_audio player id=\(playerId ?? "?")")
+ }
+ }
+ }
+ }
+
+ func register(with messenger: FlutterBinaryMessenger) {
+ let channel = FlutterMethodChannel(
+ name: "com.absorb.queue_advancer",
+ binaryMessenger: messenger
+ )
+ channel.setMethodCallHandler { [weak self] call, result in
+ self?.handle(call, result: result)
+ }
+ emit("[QueueAdvancer] channel registered")
+ }
+
+ private func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
+ let args = call.arguments as? [String: Any]
+ switch call.method {
+ case "prepareNext":
+ let title = (args?["title"] as? String) ?? ""
+ let artist = (args?["artist"] as? String) ?? ""
+ let duration = (args?["durationS"] as? Double) ?? 0
+ let coverPath = args?["coverPath"] as? String
+ queue.async { [weak self] in
+ self?._nextTitle = title
+ self?._nextArtist = artist
+ self?._nextDurationS = duration
+ self?._nextCoverPath = coverPath
+ self?.emit("[QueueAdvancer] prepared metadata for \(title)")
+ }
+ result(true)
+
+ case "commitAdvance":
+ let speed = (args?["speed"] as? Double) ?? 1.0
+ commitAdvance(speed: speed, completion: { ok in result(ok) })
+
+ case "clear":
+ queue.async { [weak self] in
+ self?._nextTitle = ""
+ self?._nextArtist = ""
+ self?._nextDurationS = 0
+ self?._nextCoverPath = nil
+ }
+ result(true)
+
+ case "isReady":
+ result(_justAudioPlayer != nil)
+
+ default:
+ result(FlutterMethodNotImplemented)
+ }
+ }
+
+ private func commitAdvance(speed: Double, completion: @escaping (Bool) -> Void) {
+ queue.async { [weak self] in
+ guard let self = self else { completion(false); return }
+ self.activateSession()
+ self.publishNowPlaying(rate: speed)
+ guard let player = self._justAudioPlayer else {
+ self.emit("[QueueAdvancer] commitAdvance: no just_audio player ref")
+ completion(false)
+ return
+ }
+ let target = Float(speed > 0 ? speed : 1.0)
+ DispatchQueue.main.async {
+ // Force the engine to re-emit audio: pause, then play and re-apply
+ // the rate explicitly. This is what manual pause+play does for users.
+ player.pause()
+ DispatchQueue.main.asyncAfter(deadline: .now() + 0.04) {
+ player.play()
+ player.rate = target
+ self.queue.async {
+ self.emit("[QueueAdvancer] commitAdvance done rate=\(player.rate) for \(self._nextTitle)")
+ // Re-publish Now Playing once the engine settles so the lock
+ // screen shows the correct playing state and elapsed time.
+ self.publishNowPlaying(rate: Double(target))
+ completion(true)
+ }
+ }
+ }
+ }
+ }
+
+ private func activateSession() {
+ let session = AVAudioSession.sharedInstance()
+ do {
+ if session.category != .playback {
+ try session.setCategory(.playback, mode: .spokenAudio, policy: .longFormAudio)
+ }
+ try session.setActive(true)
+ } catch {
+ emit("[QueueAdvancer] session activate failed: \(error.localizedDescription)")
+ }
+ }
+
+ private func publishNowPlaying(rate: Double) {
+ let title = _nextTitle
+ let artist = _nextArtist
+ let duration = _nextDurationS
+ let coverPath = _nextCoverPath
+ guard !title.isEmpty else { return }
+ var info: [String: Any] = [
+ MPMediaItemPropertyTitle: title,
+ MPMediaItemPropertyArtist: artist,
+ MPNowPlayingInfoPropertyPlaybackRate: rate,
+ MPNowPlayingInfoPropertyElapsedPlaybackTime: 0.0,
+ MPNowPlayingInfoPropertyMediaType: MPNowPlayingInfoMediaType.audio.rawValue,
+ ]
+ if duration > 0 {
+ info[MPMediaItemPropertyPlaybackDuration] = duration
+ }
+ if let coverPath = coverPath, let img = UIImage(contentsOfFile: coverPath) {
+ let artwork = MPMediaItemArtwork(boundsSize: img.size) { _ in img }
+ info[MPMediaItemPropertyArtwork] = artwork
+ }
+ DispatchQueue.main.async {
+ MPNowPlayingInfoCenter.default().nowPlayingInfo = info
+ MPNowPlayingInfoCenter.default().playbackState = rate > 0 ? .playing : .paused
+ }
+ }
+
+ private func emit(_ line: String) {
+ NSLog("%@", line)
+ Self.logSink?(line)
+ }
+}
diff --git a/lib/services/audio_player_service.dart b/lib/services/audio_player_service.dart
index 7627511a..f10797c6 100644
--- a/lib/services/audio_player_service.dart
+++ b/lib/services/audio_player_service.dart
@@ -1082,12 +1082,19 @@ class AudioPlayerService extends ChangeNotifier {
Future<void> setVolume(double v) async => _player?.setVolume(v);
static const _eqChannelForDiag = MethodChannel('com.absorb.equalizer');
+ static const _queueAdvancerChannel = MethodChannel('com.absorb.queue_advancer');
void _resetPreBufferState() {
_activeConcatSource = null;
_currentBookTrackCount = 0;
_nextBookPreloading = false;
_preloadedNextBook = null;
+ if (Platform.isIOS) {
+ _queueAdvancerChannel.invokeMethod('clear').catchError((Object e) {
+ debugPrint('[QueueAdvance] clear failed: $e');
+ return null;
+ });
+ }
}
void _maybePreloadNextBook() {
@@ -1158,6 +1165,10 @@ class AudioPlayerService extends ChangeNotifier {
}
_preloadedNextBook = next;
debugPrint('[PreBuffer] Pre-loaded next item: ${next['title']} ($nextItemId)');
+
+ if (Platform.isIOS) {
+ unawaited(_primeNativeQueueAdvancer(next));
+ }
} catch (e, st) {
debugPrint('[PreBuffer] Failed: $e\n$st');
}
@@ -1165,6 +1176,26 @@ class AudioPlayerService extends ChangeNotifier {
// play session. Reset happens in _resetPreBufferState on the next playItem.
}
+ Future<void> _primeNativeQueueAdvancer(Map<String, dynamic> next) async {
+ try {
+ String? coverPath;
+ final itemId = next['itemId'] as String?;
+ if (itemId != null) {
+ final local = DownloadService().getInfo(itemId).localCoverPath;
+ if (local != null && local.isNotEmpty) coverPath = local;
+ }
+ final ok = await _queueAdvancerChannel.invokeMethod<bool>('prepareNext', {
+ 'title': next['title'] ?? '',
+ 'artist': next['author'] ?? '',
+ 'durationS': (next['duration'] as num?)?.toDouble() ?? 0,
+ 'coverPath': coverPath,
+ });
+ debugPrint('[PreBuffer] native prepareNext returned $ok');
+ } catch (e) {
+ debugPrint('[PreBuffer] native prepareNext failed: $e');
+ }
+ }
+
void _onAutoQueueAdvanced() {
final next = _preloadedNextBook;
if (next == null) return;
@@ -1205,6 +1236,10 @@ class AudioPlayerService extends ChangeNotifier {
duration: _totalDuration,
elapsed: 0));
+ if (Platform.isIOS) {
+ unawaited(_iosAutoAdvanceKick());
+ }
+
// Notify LibraryProvider via auto-queue-specific callback (skipAutoAdvance
// inside markFinishedLocally — next item is already playing, no playItem).
if (oldKey != null) {
@@ -1216,6 +1251,35 @@ class AudioPlayerService extends ChangeNotifier {
_autoQueueAdvancing = false;
}
+ Future<void> _iosAutoAdvanceKick() async {
+ final speed = _player?.speed ?? 1.0;
+ try {
+ await (await AudioSession.instance).setActive(true);
+ } catch (e) {
+ debugPrint('[QueueAdvance] setActive failed: $e');
+ }
+ // Only run the native pause+play kick when backgrounded. In foreground the
+ // AVQueuePlayer auto-advance already produces audio, and the kick would
+ // glitch playback for no benefit.
+ if (_isBackgrounded) {
+ try {
+ await _queueAdvancerChannel.invokeMethod<bool>('commitAdvance', {
+ 'speed': speed,
+ });
+ } catch (e) {
+ debugPrint('[QueueAdvance] commitAdvance failed: $e');
+ }
+ }
+ try {
+ await _player?.setSpeed(speed);
+ } catch (e) {
+ debugPrint('[QueueAdvance] setSpeed kick failed: $e');
+ }
+ Future.delayed(const Duration(milliseconds: 300), () {
+ _handler?.refreshPlaybackState();
+ });
+ }
+
Future<void> _primeNowPlaying({
required String title,
required String artist,
diff --git a/packages/just_audio/darwin/just_audio/Sources/just_audio/AudioPlayer.m b/packages/just_audio/darwin/just_audio/Sources/just_audio/AudioPlayer.m
index 13d61a27..7649a82c 100644
--- a/packages/just_audio/darwin/just_audio/Sources/just_audio/AudioPlayer.m
+++ b/packages/just_audio/darwin/just_audio/Sources/just_audio/AudioPlayer.m
@@ -666,6 +666,13 @@
forKeyPath:@"currentItem"
options:NSKeyValueObservingOptionNew
context:nil];
+ // Hand the AVQueuePlayer to host-app native code so it can drive
+ // queue transitions directly (avoids AVQueuePlayer auto-advance
+ // losing background audio output).
+ [[NSNotificationCenter defaultCenter]
+ postNotificationName:@"AbsorbJustAudioPlayerReady"
+ object:nil
+ userInfo:@{@"player": _player, @"playerId": _playerId}];
// TODO: learn about the different ways to define weakSelf.
//__weak __typeof__(self) weakSelf = self;
//typeof(self) __weak weakSelf = self;
@@ -1392,6 +1399,10 @@
if (@available(macOS 10.12, iOS 10.0, *)) {
[_player removeObserver:self forKeyPath:@"timeControlStatus"];
}
+ [[NSNotificationCenter defaultCenter]
+ postNotificationName:@"AbsorbJustAudioPlayerReleased"
+ object:nil
+ userInfo:@{@"playerId": _playerId}];
_player = nil;
}
// Untested:
diff --git a/pubspec.yaml b/pubspec.yaml
index 098656b9..1553da7c 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,7 +1,7 @@
name: absorb
description: An Audiobookshelf Client
publish_to: 'none'
-version: 1.8.91+144
+version: 1.8.91+145
environment:
sdk: '>=3.1.0 <4.0.0'