简体   繁体   中英

AVAudioSession does not automatically connect to Bluetooth A2DP audio?

Description:

When I change my AudioSession to .playAndRecord via the following call:

AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .default, options: [.defaultToSpeaker, .allowBluetoothA2DP, .mixWithOthers])

From these category options set, I expect that, once I pair bluetooth headphones with my phone, my running app should automatically route audio to my bluetooth A2DP headphones. Instead, my app still chooses to use the phone speaker. There is no indication at all, whether from querying the audio session instance or in the form of a route change notification, that I have bluetooth headphones connected..

Question: How do I ensure the audiosession connects automatically to the bluetooth headphones?

This behavior clearly has nothing to do with an incorrect line of code. As if I had bluetooth headphones paired before the I invoke audio-session category change, my audio will correctly continue to use the bluetooth headphones.

Moreover, if I had instead used the .allowBluetooth category option (for HFP instead of A2DP) during the code invocation, and then paired bluetooth headphones, the headphones connect automatically just fine. Behavior reproduced on: iPhone XR (iOS 13.6.1) with Apple AirPods Pro iPhone XR (iOS 13.6) with Senso wireless 44.1kHz bluetooth headphones.

Reproduction steps:

For the sake of simplicity and reproducibility for Apple's engineers, I have reproduced the issue on a lightly modified version of AVEchoTouch Demo Code from WWDC . As this was a demo project published by Apple, it is all correct code. My modified project, called BluetoothIssue , can be found here .

Step 1: Make sure your bluetooth headphones are currently not paired with the iPhone.

Step 2: Launch the “BluetoothIssue App”. When you toggle the “FX Out” or the “Speech Out” toggles, the app should be playing sounds (out of iPhone speaker)

Step 3: Now connect your bluetooth headphones to the phone. Even though the headphones are now connected and paired with phone, the app does not register the connection, and continues play out of the phones built-in speakers.

// Steps 4-5 (Optional): to see that nothing is wrong with the app's category itself

Step 4: Now force kill the “BluetoothIssue” app, while your headphones are still connected.

Step 5: Now relaunch the “BluetoothIssue” app. The app plays sound correctly out to the bluetooth headphones.

I learned that if i set the mode to VideoRecording, iOS will change the route if I turn the bluetooth on (and I get a route change notification). Without this mode I had to allow both bluetooth, after detect one, change the category again without AllowBluetooth, just A2DP. Weird and slow solution, but worked. Now im just using videorecording mode.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM