根据苹果文档,在后台运行的应用程序无法接收回调,除了基于位置、VOIP、Newstand、音乐播放器或启用 APNS 的应用程序。在这种情况下如何接收连接状态回调?该应用程序将 AVAudioSession 实例用作:
try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord, with:AVAudioSessionCategoryOptions.allowBluetooth)
并以如下方式收听通知:
NotificationCenter.default.addObserver(self,selector:#selector(handleRouteChange), name: .AVAudioSessionRouteChange,object: AVAudioSession.sharedInstance())
这在应用程序的启动中被调用。