【发布时间】:2017-12-14 15:21:12
【问题描述】:
我的应用似乎不适用于 AirPods。现在我正在使用此代码进行播放和录制:
let audioSession = AVAudioSession.sharedInstance()
do { try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.defaultToSpeaker)
}catch {
print("audioSession properties weren't set because of an error.")
}
我把defaultToSpeaker改成allowBluetooth就够了吗?
附:我知道这是一个非常愚蠢的问题,因为更改这条线并检查会更简单,但我现在没有 AirPods,所以我唯一的选择是将新版本上传到 Testflight(和我想以最少的迭代次数来做到这一点)。
更新:(相当幼稚的方法——但我只需要使用可用的蓝牙耳机):
func selectDevice(audioSession: AVAudioSession) {
var headphonesExist = false
var bluetoothExist = false
var speakerExist = false
let currentRoute = AVAudioSession.sharedInstance().currentRoute
for output in audioSession.currentRoute.outputs {
print(output)
if output.portType == AVAudioSessionPortHeadphones || output.portType == AVAudioSessionPortHeadsetMic {
headphonesExist = true
}
if output.portType == AVAudioSessionPortBluetoothA2DP || output.portType == AVAudioSessionPortBluetoothHFP {
bluetoothExist = true
}
if output.portType == AVAudioSessionPortBuiltInSpeaker {
speakerExist = true
}
}
if bluetoothExist == true {
do { try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.allowBluetooth) } catch {
print("error with audiosession: bluetooth")
}
}
}
【问题讨论】:
标签: ios swift audio avfoundation avaudiosession