1
我試圖在用戶選擇操作表上的選項時將音頻輸出更改爲設備。這是當我選擇音頻去設備,藍牙不會出現下一次:將音頻更改爲藍牙設備並返回
for input in AVAudioSession.sharedInstance().availableInputs!{
if input.portType == AVAudioSessionPortBluetoothA2DP || input.portType == AVAudioSessionPortBluetoothHFP || input.portType == AVAudioSessionPortBluetoothLE{
let bluetooth = UIAlertAction(title: input.portName, style: .default, handler: {
(alert: UIAlertAction!) -> Void in
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.allowBluetooth)
try audioSession.setActive(true)
} catch {
fatalError("Error Setting Up bluetooth output \(input.portName)")
}
})
bluetooth.setValue(UIImage(named:"bluetooth.png"), forKey: "image")
optionMenu.addAction(bluetooth)
}
let iphomeOutput = UIAlertAction(title: "iPhone", style: .default, handler: {
(alert: UIAlertAction!) -> Void in
let audioSession = AVAudioSession.sharedInstance()
do {
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.duckOthers)
try audioSession.setActive(true)
} catch {
fatalError("Error Setting Up audio output Phone")
}
try audioSession.overrideOutputAudioPort(AVAudioSessionPortOverride.none)
} catch let error as NSError {
print("audioSession error turning off speaker: \(error.localizedDescription)")
}
})
for description in currentRoute.outputs {
if description.portType == AVAudioSessionPortHeadsetMic{
optionMenu.setValue(true, forKey: "checked")
break
}
}
optionMenu.addAction(iphomeOutput)
這太神奇了。謝謝。 –
不錯,謝謝。 – FreeGor