0

I have an avassetwriter capture session to record a video and 2 avplayerqueues to playback the immediately recorded video and the other, to playback past saved videos.

My problem is the audio input does not use my bluetooth earphones, and performs playback and record via the iphone device inputs/outputs.

I did not implement any override to default to the speaker, understand I need to handle a route change via notification observer in the case of toggling between bluetooth and device, and also tried setting the AVAudioSession.sharedInstance() .playbackAndRecording category to .allowBluetooth to no avail.

Any guidance would be appreciated as I have not come across an existing answer online..

let audioSession = AVAudioSession.sharedInstance()
    
    //Executed right before playing avqueueplayer media
    do {
        try audioSession.setCategory(.playback)
        try audioSession.setActive(true)
    } catch {
        fatalError("Error Setting Up Audio Session")
    }


    //Executed right after avqueueplayer finishes media
    do {
        try audioSession.setCategory(.recording, options: [.allowBluetooth])
        try audioSession.setActive(true)
    } catch {
        fatalError("Error Setting Up Audio Session")
    }
Chris
  • 431
  • 8
  • 33
  • https://stackoverflow.com/questions/29835019/how-add-avcapturedeviceinput-to-avcapturesession-from-bluetooth-device this solved it for me. – Chris Nov 09 '21 at 20:54

1 Answers1

2

Did you read carefully the documentation for .allowBluetooth and .allowBluetoothA2DP? These do not mean what you likely think they mean, since passing both is rarely what developers mean. Do not guess what AVFoundation will do based on the names of things; you must read the documentation. The names of things are not obvious, and are actively misleading in several places.

If you want to record from Bluetooth headphones, you cannot support A2DP. A2DP is a unidirectional protocol (playback only). You will need to use HFP, which is what .allowBluetooth means. Remove .allowBluetoothA2DP. Note that this is significantly reduce your audio quality.

If you have distinct periods of recording vs playback, then you want to change your category when you change modes. Do not just set .playAndRecord because you will record at some point and playback at another. If you switch to a playback-only situation, switch to .playback. It is legal to change categories while the session is active (again, see the docs; there are many subtle rules).

You haven't listed what your Bluetooth earphones are, so it's not clear whether they support both A2DP and HFP. That has significant impact on how routing occurs. See the docs for .allowBluetoothA2DP on this.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • 1
    appreciate the clarification Rob. i am using airpods to test and updated my code to switch between .playback and .recording categories, but am still faced the same issue. after more digging, i believe it's because my avCaptureSession is running simultaenously and i had .automaticallyConfiguresApplicationAudioSession set to true. changing it to false finally allowed playback through bluetooth, although now im running into a different issue where the captureSession displays a blank screen and doesn't record anything.. – Chris Nov 08 '21 at 23:57
  • Yeah; definitely if you have some other thing configuring the session, that's going to cause it to behave differently than expected :D If AVCaptureSession needs different settings than your playback session, then you'll need to switch the category, categoryOptions, and mode whenever you switch between them. You can check what AVCaptureSession sets them to and save that off, and reapply it before activating the capture. Or you can just configure it all yourself; you'll need to study the docs on the categories, options, and modes, though. – Rob Napier Nov 09 '21 at 13:54