问题
Im using iOS app that stream from url (radio app), once im trying to stream from the app through Bluetooth devise like external speaker or car audio system, the audio quality is vert poor and jarring. When playing from the iOS devise itself, everything sounds good (speaker and earphones).
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)")
}
回答1:
Thanks to Michal Cichon's comment this code finally works for me and the bluetooth isn't distorted
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
}
回答2:
I ran into a similar issue and I changed the category to AVAudioSessionCategoryPlayback to fix it.
来源:https://stackoverflow.com/questions/38947490/ios-swift-poor-audio-quality-when-using-bluetooth-external-speaker