iOS Swift - Poor audio quality when using Bluetooth external speaker

旧街凉风 提交于 2021-01-28 00:18:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!