Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Add an AirPlay button to the player on iPhone and iPad

Barnabas37d5931413d2
diff --git a/ios/Runner/AppDelegate.swift b/ios/Runner/AppDelegate.swift
index 6e9bb83a..bf2fd08f 100644
--- a/ios/Runner/AppDelegate.swift
+++ b/ios/Runner/AppDelegate.swift
@@ -3,6 +3,7 @@ import AppIntents
import Flutter
import UIKit
import AVFoundation
+import AVKit
import MediaPlayer
import just_audio
@@ -275,7 +276,8 @@ let flutterEngine = FlutterEngine(name: "SharedEngine", project: nil, allowHeadl
// 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.
+ // pick output devices directly. Stub those, but do expose the AirPlay
+ // picker (the one piece the in-app button needs).
let channel = FlutterMethodChannel(name: "com.absorb.audio_output",
binaryMessenger: messenger)
channel.setMethodCallHandler { (call, result) in
@@ -284,6 +286,34 @@ let flutterEngine = FlutterEngine(name: "SharedEngine", project: nil, allowHeadl
result([])
case "setAudioOutputDevice", "resetAudioOutput":
result(false)
+ case "showRoutePicker":
+ // There is no public API to present the AirPlay picker directly, so
+ // add a transient AVRoutePickerView to the key window and tap its
+ // button. The view is removed again shortly after.
+ DispatchQueue.main.async {
+ let scenes = UIApplication.shared.connectedScenes
+ .compactMap { $0 as? UIWindowScene }
+ let host = scenes.flatMap { $0.windows }.first { $0.isKeyWindow }
+ ?? scenes.flatMap { $0.windows }.first
+ guard let window = host else { result(false); return }
+ let picker = AVRoutePickerView(frame: CGRect(x: -1000, y: -1000, width: 0, height: 0))
+ picker.prioritizesVideoDevices = false
+ window.addSubview(picker)
+ func findButton(_ view: UIView) -> UIButton? {
+ if let b = view as? UIButton { return b }
+ for sub in view.subviews { if let b = findButton(sub) { return b } }
+ return nil
+ }
+ var triggered = false
+ if let button = findButton(picker) {
+ button.sendActions(for: .touchUpInside)
+ triggered = true
+ }
+ DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
+ picker.removeFromSuperview()
+ }
+ result(triggered)
+ }
default:
result(FlutterMethodNotImplemented)
}
diff --git a/lib/services/player_settings.dart b/lib/services/player_settings.dart
index e4b0fe8c..0b391fde 100644
--- a/lib/services/player_settings.dart
+++ b/lib/services/player_settings.dart
@@ -523,7 +523,7 @@ class PlayerSettings {
// ── Card button order ──
- static const defaultButtonOrder = ['chapters', 'speed', 'sleep', 'bookmarks', 'details', 'equalizer', 'cast', 'history', 'remove', 'car', 'notes', 'download'];
+ static const defaultButtonOrder = ['chapters', 'speed', 'sleep', 'bookmarks', 'details', 'equalizer', 'cast', 'airplay', 'history', 'remove', 'car', 'notes', 'download'];
static Future<List<String>> getCardButtonOrder() async {
final stored = await ScopedPrefs.getStringList('card_button_order');
diff --git a/lib/widgets/card_button_config.dart b/lib/widgets/card_button_config.dart
index b5395bfd..fa8a5b55 100644
--- a/lib/widgets/card_button_config.dart
+++ b/lib/widgets/card_button_config.dart
@@ -29,6 +29,8 @@ String localizedCardButtonLabel(AppLocalizations l, CardButtonDef def) {
return l.equalizerLabel;
case 'cast':
return l.castToDevice;
+ case 'airplay':
+ return 'AirPlay';
case 'history':
return l.playbackHistory;
case 'remove':
@@ -50,6 +52,8 @@ String localizedCardButtonLabel(AppLocalizations l, CardButtonDef def) {
final Set<String> _hiddenButtons = {
if (Platform.isIOS) 'cast',
if (!ChromecastService.castSupported) 'cast',
+ // AirPlay is iOS-only (Chromecast covers Android).
+ if (!Platform.isIOS) 'airplay',
};
const _allCardButtons = [
@@ -60,6 +64,7 @@ const _allCardButtons = [
CardButtonDef('details', 'Book Details', Icons.info_outline_rounded),
CardButtonDef('equalizer', 'Equalizer', Icons.equalizer_rounded),
CardButtonDef('cast', 'Cast to Device', Icons.cast_rounded),
+ CardButtonDef('airplay', 'AirPlay', Icons.airplay_rounded),
CardButtonDef('history', 'Playback History', Icons.history_rounded),
CardButtonDef('remove', 'Remove from Absorbing', Icons.remove_circle_outline_rounded),
CardButtonDef('car', 'Car Mode', Icons.directions_car_rounded),
diff --git a/lib/widgets/card_buttons.dart b/lib/widgets/card_buttons.dart
index 587bb321..54863bdf 100644
--- a/lib/widgets/card_buttons.dart
+++ b/lib/widgets/card_buttons.dart
@@ -1578,6 +1578,12 @@ class CardActionDelegate {
);
},
);
+ case 'airplay':
+ return CardWideButton(
+ icon: Icons.airplay_rounded, label: 'AirPlay',
+ accent: accent, isActive: true, alwaysEnabled: true, large: large, compact: compact, iconsOnly: iconsOnly,
+ onTap: () => handleAirPlayTap(),
+ );
case 'history':
return CardWideButton(
icon: Icons.history_rounded, label: (compact || short) ? l.historyShort : l.playbackHistory,
@@ -1691,6 +1697,11 @@ class CardActionDelegate {
);
},
);
+ case 'airplay':
+ return MoreMenuItem(
+ icon: Icons.airplay_rounded, label: 'AirPlay', accent: accent,
+ onTap: () { Navigator.pop(ctx); handleAirPlayTap(); },
+ );
case 'history':
return MoreMenuItem(
icon: Icons.history_rounded, label: l.playbackHistory, accent: accent,
@@ -1811,6 +1822,14 @@ class CardActionDelegate {
));
}
+ /// iOS only: open the system AirPlay route picker. Routed through the native
+ /// AVRoutePickerView (see AppDelegate's com.absorb.audio_output channel).
+ static const MethodChannel _audioOutputChannel =
+ MethodChannel('com.absorb.audio_output');
+ void handleAirPlayTap() {
+ _audioOutputChannel.invokeMethod('showRoutePicker').catchError((_) => null);
+ }
+
void handleCastTap(BuildContext ctx, Color accent) {
final cast = ChromecastService();
final auth = ctx.read<AuthProvider>();