这是我一直在寻找的控制:https://developer.apple.com/documentation/avkit/avroutepickerview。它在屏幕上显示为一个按钮(带有默认的AirPlay 2图标)并为您处理所有逻辑。
例
import AVKit
let audioPicker = AVRoutePickerView(frame: ...)
addSubview(audioPicker)
您可以使用以下内容订阅音频路由更新:
NotificationCenter.default.addObserver(self, selector: ...), name: AVAudioSession.routeChangeNotification, object: nil)