当蓝牙连接打开/关闭时,AVAudioSessionRouteChange音频套件崩溃

当蓝牙连接打开/关闭时,AVAudioSessionRouteChange音频套件崩溃,ios,bluetooth,avfoundation,audiokit,Ios,Bluetooth,Avfoundation,Audiokit,我使用AVFoundation/AudioKit录制iPhone/iPad的内部麦克风。在蓝牙A2DP和内置扬声器之间切换输出后,应该可以继续使用应用程序。麦克风应继续接收设备内部麦克风的输入。确实如此。一切都很好,但直到我想改变输出设备 func basicAudioSetup(){ // microphone self.microphone = AKMicrophone() // select input of device if let input =

我使用AVFoundation/AudioKit录制iPhone/iPad的内部麦克风。在蓝牙A2DP和内置扬声器之间切换输出后,应该可以继续使用应用程序。麦克风应继续接收设备内部麦克风的输入。确实如此。一切都很好,但直到我想改变输出设备

func basicAudioSetup(){

    // microphone
    self.microphone = AKMicrophone()

    // select input of device
    if let input = AudioKit.inputDevice {
        try! self.microphone?.setDevice(input)
    }

    AKSettings.sampleRate = 44100
    AKSettings.channelCount = 2
    AKSettings.playbackWhileMuted = true
    AKSettings.enableRouteChangeHandling = false
    AKSettings.useBluetooth = true
    AKSettings.allowAirPlay = true
    AKSettings.defaultToSpeaker = true
    AKSettings.audioInputEnabled = true


    // init DSP

    self.dsp = AKClock(amountSamples: Int32(self.amountSamples), amountGroups: Int32(self.amountGroups), bpm: self.bpm, iPad:self.iPad)


    self.masterbusTracker = AKAmplitudeTracker(self.dsp)
    self.mixer.connect(input: self.masterbusTracker)

    self.player = AKPlayer()
    self.mixer.connect(input: self.player)


    self.microphone?.stop()
    self.microphoneTracker = AKAmplitudeTracker(self.microphone)
    self.microphoneTracker?.stop()
    self.microphoneRecorder = try! AKNodeRecorder(node: self.microphone)
    self.microphoneMonitoring = AKBooster(self.microphoneTracker)
    self.microphoneMonitoring?.gain = 0
    self.mixer.connect(input: self.microphoneMonitoring)

    AudioKit.output = self.mixer

    // the following line is actually happening inside a customized AudioKit.start() function to make sure that only BluetoothA2DP is used for better sound quality:
    try AKSettings.setSession(category: .playAndRecord, with: [.defaultToSpeaker, .allowBluetoothA2DP, .allowAirPlay, .mixWithOthers])

    do {
        try AudioKit.start()
    }catch{
        print("AudioKit did not start")
    }



     // adding Notifications to manually restart the engine.

     NotificationCenter.default.addObserver(self, selector: #selector(self.audioRouteChangeListener(notification:)), name: NSNotification.Name.AVAudioSessionRouteChange, object: nil)

}

@objc func audioRouteChangeListener(notification:NSNotification) {

    let audioRouteChangeReason = notification.userInfo![AVAudioSessionRouteChangeReasonKey] as! UInt

    let checkRestart = {

        print("ROUTE CHANGE")

        do{
           try AudioKit.engine.start()
        }catch{
           print("error rebooting engine")
        }

    }

    if audioRouteChangeReason == AVAudioSessionRouteChangeReason.newDeviceAvailable.rawValue ||
        audioRouteChangeReason == AVAudioSessionRouteChangeReason.oldDeviceUnavailable.rawValue{

        if Thread.isMainThread {
            checkRestart()
        } else {
            DispatchQueue.main.async(execute: checkRestart)
        }

    }

}
我注意到,当连接麦克风时,从内置扬声器切换到蓝牙时,不会调用AVAudioSessionRouteChange。启动蓝牙并从蓝牙切换到内置扬声器时,我确实会收到信息:

[AVAudioEngineGraph.mm:1481:Start:(err=PerformCommand(*ioNode, 考斯塔蒂奥,空,0)

这个消息到底是什么意思?我尝试了一切,从手动断开引擎/de的所有输入,重新激活会话到重建整个链。什么都不起作用

理论上,输入源不会改变,因为它保留在手机的输入上。非常感谢您的帮助


仅供参考:我正在使用自定义版本的AudioKit库,其中我删除了其AVAudioSessionRouteChange的内部通知,以避免不必要的Doppelgaenger。出于同样的原因,该自定义库还在内部设置会话类别和选项,以确保只使用BluetoothA2DP。

您找到了解决此问题的方法吗什么问题?