我正在使用从 url (广播应用程序)流式传输的 iOS 应用程序,
一旦我尝试通过外部扬声器或汽车音响系统等蓝牙设备从应用程序进行流传输,音频质量就会非常差且刺耳。
当从 iOS 设备本身播放时,一切听起来都很好(扬声器和耳机)。
override func viewDidLoad() {
super.viewDidLoad()
// Set AVFoundation category, required for background audio
var error: NSError?
var success: Bool
do {
try AVAudioSession.sharedInstance().setCategory(
AVAudioSessionCategoryPlayAndRecord,
withOptions: [
AVAudioSessionCategoryOptions.AllowBluetooth,
AVAudioSessionCategoryOptions.DefaultToSpeaker])
success = true
} catch let error1 as NSError {
error = error1
success = false
}
if !success {
print("Failed to set audio session category. Error: \(error)")
}
感谢 Michal Cichon 的评论,这段代码终于对我有用了,并且蓝牙没有失真
do{
if #available(iOS 10.0, *) {
try AVAudioSession.sharedInstance().setCategory(.playAndRecord,
mode: .default, options: [.mixWithOthers, .allowAirPlay,
.allowBluetoothA2DP,.defaultToSpeaker])
print("ios 10 and above")
} else {
print("ios 10 and lower")
let options: [AVAudioSession.CategoryOptions] =
[.mixWithOthers, .allowBluetooth]
let category = AVAudioSession.Category.playAndRecord
let selector =
NSSelectorFromString("setCategory:withOptions:error:")
AVAudioSession.sharedInstance().perform(selector, with:
category, with: options)
}
try AVAudioSession.sharedInstance().setActive(true)
captureSession.automaticallyConfiguresApplicationAudioSession =
false
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)