Or Assraf
Or Assraf

Reputation: 634

iOS Swift - Poor audio quality when using Bluetooth external speaker

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)")
    }

Upvotes: 4

Views: 1636

Answers (2)

landnbloc
landnbloc

Reputation: 1068

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
         }

Upvotes: 2

Louie
Louie

Reputation: 11

I ran into a similar issue and I changed the category to AVAudioSessionCategoryPlayback to fix it.

Upvotes: 0

Related Questions