我的应用程序似乎不适用于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")
}
}
}发布于 2018-06-23 04:44:08
您需要在options参数中添加对蓝牙的支持,如下所示:
[AVAudioSessionCategoryOptions.defaultToSpeaker, .allowBluetoothA2DP]
.allowBluetoothA2DP将允许向蓝牙设备提供高质量的音频输出,并限制所述设备上的麦克风输入,而.allowBluetooth将默认HFP兼容(输入/输出)蓝牙设备为支持麦克风输入的低质量HFP。
发布于 2020-10-27 15:56:24
以下是请求适当许可的完整来源。您所要做的就是添加一个带有“.allowBluetoothA2DP”的模式
我申请了Swift 5
func requestPermissionForAudioRecording(){
recordingSession = AVAudioSession.sharedInstance()
do {
// only with this without options, will not capable with your Airpod, the Bluetooth device.
// try recordingSession.setCategory(.playAndRecord, mode: .default)
try recordingSession.setCategory(.playAndRecord, mode: .default, options: [.defaultToSpeaker, .allowAirPlay, .allowBluetoothA2DP])
try recordingSession.setActive(true)
recordingSession.requestRecordPermission() { allowed in
DispatchQueue.main.async {
if allowed {
// Recording permission has been allowed
self.recordingPermissionGranted = true
} else {
// failed to record!
}
}
}
} catch let err {
// failed to record!
print("AudioSession couldn't be set!", err)
}
}https://stackoverflow.com/questions/47816753
复制相似问题