4

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)")
    }
Or Assraf
  • 564
  • 1
  • 4
  • 12

2 Answers2

2

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
         }
landnbloc
  • 498
  • 4
  • 10
  • Where is captureSession coming from? I cannot see that variable declared. Thx – Gergely Kovacs Feb 17 '21 at 23:27
  • It's a AVCaptureSession for recording video, this code should probably be in your AppDelegate while the capture session (if there is one) should be in a different controller. This must be how I had it at first before it went to AppDelegate. So good question lol – landnbloc Feb 18 '21 at 16:59
0

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

Louie
  • 11
  • 4
    Since iOS 10 you should set mode `.allowBluetoothA2DP` instead of `.allowBluetooth` – Michal Cichon Oct 03 '18 at 20:10
  • @Louie that gives another error, since AVAudioSession is becoming nil. t still plays in the background though. Strange. Improves the sound back to normal though. – Gergely Kovacs Feb 17 '21 at 23:29
  • @Louie, this great concern of mine was finally solved it by leaving options array empty, or only including .mixWithOthers! Still works with Hands Free Bluetooth ! – Gergely Kovacs Feb 17 '21 at 23:36