当我将 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: ()
}
}