han*_*sen 2 bluetooth ios avaudiosession swift
我的蓝牙播放有问题,但仅限于某些蓝牙设备。其他应用程序在这些相同的蓝牙设备上运行良好。我设置类别的方式是否遗漏了什么AVAudioSession?
let session = AVAudioSession.sharedInstance()
do {
try session.setCategory(AVAudioSessionCategoryPlayAndRecord, with: [.defaultToSpeaker, .allowBluetooth, .allowAirPlay])
try session.setActive(true)
session.requestRecordPermission({(granted:Bool) in
if granted {
Variables.appHasMicAccess = true
} else {
Variables.appHasMicAccess = false
}
})
} catch let error as NSError {
print("AVAudioSession configuration error: \(error.localizedDescription)")
}
Run Code Online (Sandbox Code Playgroud)
解决方案是添加一个附加选项.allowBluetoothA2DP。这是在 iOS 10 中引入的。
从 iOS 10.0 开始,使用 playAndRecord 类别的应用程序也可能允许将输出路由到配对的蓝牙 A2DP 设备。要启用此行为,您需要在设置音频会话的类别时传递此类别选项。
更多细节在这里
如果在使用 A2DP 的扬声器上播放,请考虑如下设置会话:
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord,
mode: AVAudioSessionModeVideoRecording,
options: [.defaultToSpeaker, .allowAirPlay, .allowBluetoothA2DP])
try audioSession.setActive(true)
} catch let error {
print("audioSession properties weren't set!", error)
}
Run Code Online (Sandbox Code Playgroud)
注意选项“.allowBluetoothA2DP”
希望有帮助
| 归档时间: |
|
| 查看次数: |
1900 次 |
| 最近记录: |