音频切换到扬声器不会停留

时间:2019-03-20 13:18:35

标签: swift bluetooth avaudiosession speaker

当我连接了Airpods时,不会保持将音频切换到扬声器的功能。此代码在除连接Airpods之外的所有状态下均有效。

var err: Error? = nil
        let session = AVAudioSession.sharedInstance()
        do {
            try session.setCategory(AVAudioSession.Category.playAndRecord, mode: .voiceChat, options: [.allowBluetooth, .allowBluetoothA2DP, .mixWithOthers])

        } catch {
            NSLog("Unable to change audio category because : \(String(describing: err?.localizedDescription))")
            err = nil
        }

        try? session.setMode(AVAudioSession.Mode.voiceChat)
        if err != nil {
            NSLog("Unable to change audio mode because : \(String(describing: err?.localizedDescription))")
            err = nil
        }
        let sampleRate: Double = 44100.0
        try? session.setPreferredSampleRate(sampleRate)
        if err != nil {
            NSLog("Unable to change preferred sample rate because : \(String(describing: err?.localizedDescription))")
            err = nil
        }
        try? session.setPreferredIOBufferDuration(0.005)
        if err != nil {
            NSLog("Unable to change preferred sample rate because : \(String(describing: err?.localizedDescription))")
            err = nil
        }

当选择UIAlertAction行时:

  do {
                        try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSession.PortOverride.speaker)
                    } catch let error as NSError {
                        print("audioSession error turning off speaker: \(error.localizedDescription)")
                    }

0 个答案:

没有答案