Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Take the headset claim back when the headphones disconnect while paused

Barnabas9aafa324485e
diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swift
index f0e9c3e4..703c024a 100644
--- a/ios/Runner/AppDelegate.swift
+++ b/ios/Runner/AppDelegate.swift
@@ -191,12 +191,22 @@ let flutterEngine = FlutterEngine(name: "SharedEngine", project: nil, allowHeadl
let opts = AVAudioSession.InterruptionOptions(rawValue: optionsRaw)
details.append("shouldResume=\(opts.contains(.shouldResume))")
}
+ var reasonRaw: UInt = 0
if #available(iOS 14.5, *) {
- if let reasonRaw = note.userInfo?[AVAudioSessionInterruptionReasonKey] as? UInt {
- details.append("reasonRaw=\(reasonRaw)")
+ if let r = note.userInfo?[AVAudioSessionInterruptionReasonKey] as? UInt {
+ reasonRaw = r
+ details.append("reasonRaw=\(r)")
}
}
self?.logToFlutter("[AudioSession] interruption \(details.joined(separator: " "))")
+ // reason 4 = routeDisconnected: the headphones left, so iOS tore the
+ // session down as an "interruption" that never gets an ended event -
+ // waiting for one leaves the Now Playing claim dead and the next
+ // headset press goes to Apple Music. There is no interrupter to yield
+ // to here, so Dart takes the claim back right away.
+ if typeName == "began", reasonRaw == 4 {
+ self?.widgetChannel?.invokeMethod("reassertClaim", arguments: nil)
+ }
}
nc.addObserver(
diff --git a/lib/services/home_widget_service.dart b/lib/services/home_widget_service.dart
index bc1db468..9d52245f 100644
--- a/lib/services/home_widget_service.dart
+++ b/lib/services/home_widget_service.dart
@@ -101,6 +101,16 @@ class HomeWidgetService {
} else if (call.method == 'log') {
final msg = (call.arguments as Map?)?['msg'] as String?;
if (msg != null) debugPrint('[WidgetDebug] $msg');
+ } else if (call.method == 'reassertClaim') {
+ // Native saw the session torn down with no ended event coming
+ // (headphones disconnected while paused) - take the claim back so
+ // the next press still reaches this app. Slight delay so the route
+ // change finishes settling first.
+ Future.delayed(const Duration(milliseconds: 1500), () {
+ AudioPlayerService().reassertIosClaimWhilePaused(
+ 'route disconnected',
+ );
+ });
}
return null;
});