1

当我将 airpods 连接到我的 iphone 并尝试将音频覆盖到扬声器时,音频默认返回到 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
        }

行动表上的演讲者行:

let speakerOutput = UIAlertAction(title: "Speaker", style: .default, handler: {
            (alert: UIAlertAction!) -> Void in

                self.overrideSpeaker(override: true)
        })
        for description in currentRoute.outputs {
            if convertFromAVAudioSessionPort(description.portType) == convertFromAVAudioSessionPort(AVAudioSession.Port.builtInSpeaker){
                speakerOutput.setValue(true, forKey: "checked")
                break
            }
        }
        speakerOutput.setValue(UIImage(named: "ActionSpeaker.png")?.withRenderingMode(.alwaysOriginal), forKey: "image")
        optionMenu.addAction(speakerOutput)

我在这里换了演讲者,布尔确实是真实的:

func overrideSpeaker(override : Bool) {
        do {
            let port: AVAudioSession.PortOverride = override ? .speaker : .none
            try session.overrideOutputAudioPort(port)
        } catch {
            NSLog("audioSession error toggling speaker: \(error.localizedDescription)")
        }
    }

这是我的路由更改委托,我首先得到覆盖,然后是 newDeviceAvailable:

@objc func handleRouteChange(_ notification: Notification) {
guard let userInfo = notification.userInfo,
    let reasonValue = userInfo[AVAudioSessionRouteChangeReasonKey] as? UInt,
    let reason = AVAudioSession.RouteChangeReason(rawValue:reasonValue) else {
        return
}
switch reason {
case .newDeviceAvailable,
     .categoryChange:
    var audioShown = false


    for output in session.currentRoute.outputs where output.portType != AVAudioSession.Port.builtInReceiver && output.portType != AVAudioSession.Port.builtInSpeaker {
        self.showAudio()
        audioShown = true
        break
    }
    if !audioShown {
        self.showSpeaker()
    }
    break
case .routeConfigurationChange:
    break
case .override:
    break
default: ()
}

}

4

1 回答 1

1

尽管这不是最佳答案,但您可以尝试在调用 overrideOutputAudioPort() 之前调用 setCategory() ,并且当您这样做时,省略 .allowBluetooth 选项。然后,如果他们取消选中扬声器,您将不得不将其放回原处。

使用 AVAudioSession.currentRoute.availableInputs 中的元数据,您可以将此逻辑的使用限制为仅在用户连接了 airpods 时使用。

于 2019-11-04T20:18:43.240 回答