我正在使用从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)")
}
答案 0 :(得分:1)
感谢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
}
答案 1 :(得分:0)
我遇到了类似的问题,我将类别更改为AVAudioSessionCategoryPlayback来修复它。