AVAudioSession 在某些蓝牙设备上不工作
AVAudioSession Not Working on Certain Bluetooth Devices
我在蓝牙播放方面遇到问题,但仅限于某些蓝牙设备。其他应用程序在这些相同的蓝牙设备上运行良好。我为 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)")
}
更新
解决方案是添加一个附加选项 .allowBluetoothA2DP
。这是在 iOS 10 中引入的。
Starting with iOS 10.0, apps using the playAndRecord category may also allow routing output to paired Bluetooth A2DP devices. To enable this behavior, you need to pass this category option when setting your audio session's category.
更多详情here
如果在使用 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)
}
注意选项“.allowBluetoothA2DP”
希望对您有所帮助
我在蓝牙播放方面遇到问题,但仅限于某些蓝牙设备。其他应用程序在这些相同的蓝牙设备上运行良好。我为 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)")
}
更新
解决方案是添加一个附加选项 .allowBluetoothA2DP
。这是在 iOS 10 中引入的。
Starting with iOS 10.0, apps using the playAndRecord category may also allow routing output to paired Bluetooth A2DP devices. To enable this behavior, you need to pass this category option when setting your audio session's category.
更多详情here
如果在使用 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)
}
注意选项“.allowBluetoothA2DP”
希望对您有所帮助