Ios A音频会话未检测到已连接的蓝牙设备
我正在使用以下代码检测可用的输出,但它没有拾取我连接的蓝牙设备。我知道我的蓝牙设备已正确连接,因为我正在播放来自它的音乐Ios A音频会话未检测到已连接的蓝牙设备,ios,iphone,swift,bluetooth,avaudioplayer,Ios,Iphone,Swift,Bluetooth,Avaudioplayer,我正在使用以下代码检测可用的输出,但它没有拾取我连接的蓝牙设备。我知道我的蓝牙设备已正确连接,因为我正在播放来自它的音乐 let session = AVAudioSession.sharedInstance() for description in session.currentRoute.outputs { if description.portType == AVAudioSessionPortHeadphones {
let session = AVAudioSession.sharedInstance()
for description in session.currentRoute.outputs {
if description.portType == AVAudioSessionPortHeadphones {
NSLog("headphone plugged in")
let alertActin = UIAlertAction(title: "Headphones", style: UIAlertActionStyle.default, handler:nil)
alertActin.setValue(UIColor.blue, forKey: "titleTextColor")
alert.addAction(alertActin)
} else if description.portType == AVAudioSessionPortBluetoothA2DP {
NSLog("Bluetooth plugged in")
let alertActin = UIAlertAction(title: description.portName, style: UIAlertActionStyle.default, handler:nil)
alertActin.setValue(UIColor.blue, forKey: "titleTextColor")
alert.addAction(alertActin)
} else if description.portType == AVAudioSessionPortBluetoothLE {
NSLog("Bluetooth plugged in")
let alertActin = UIAlertAction(title: description.portName, style: UIAlertActionStyle.default, handler:nil)
alertActin.setValue(UIColor.blue, forKey: "titleTextColor")
alert.addAction(alertActin)
}
else {
NSLog("")
}
}
也许您需要指示AVAudioSession首先按照建议允许蓝牙输出
请注意,讨论了AvaudioSessionCategoryPlay和Record的使用,并建议在使用输出设备时使用AvaudioSessionCategoryPlay和Record。我已将此添加到代码中,但我的蓝牙扬声器仍然没有出现,请执行{try session.setCategory(AvaudioSessionCategoryPlay和Record,带:[.allowBluetooth])try session.setActive(true)}catch{fatalError(“设置音频会话时出错”)}