简体   繁体   English

AVAudioSession 不会自动连接蓝牙 A2DP 音频?

[英]AVAudioSession does not automatically connect to Bluetooth A2DP audio?

Description:描述:

When I change my AudioSession to .playAndRecord via the following call:当我改变我的AudioSession.playAndRecord通过以下电话:

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.从这些类别选项设置中,我希望,一旦我将蓝牙耳机与手机配对,我正在运行的应用程序应该会自动将音频路由到我的蓝牙 A2DP 耳机。 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.此外,如果我在代码调用期间使用.allowBluetooth类别选项(用于 HFP 而不是 A2DP),然后配对蓝牙耳机,耳机会自动连接就好了。 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.行为再现:配备 Apple AirPods Pro 的 iPhone XR (iOS 13.6.1) 配备 Senso 无线 44.1kHz 蓝牙耳机的 iPhone XR (iOS 13.6)。

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 .为了 Apple 工程师的简单性和可重复性,我在WWDCAVEchoTouch 演示代码的轻微修改版本上重现了该问题。 As this was a demo project published by Apple, it is all correct code.由于这是 Apple 发布的演示项目,因此所有代码都是正确的。 My modified project, called BluetoothIssue , can be found here .我修改后的项目,称为BluetoothIssue ,可以在这里找到。

Step 1: Make sure your bluetooth headphones are currently not paired with the iPhone.第 1 步:确保您的蓝牙耳机当前未与 iPhone 配对。

Step 2: Launch the “BluetoothIssue App”.第 2 步:启动“BluetoothIssue App”。 When you toggle the “FX Out” or the “Speech Out” toggles, the app should be playing sounds (out of iPhone speaker)当您切换“FX Out”或“Speech Out”切换时,应用程序应该正在播放声音(从 iPhone 扬声器中)

Step 3: Now connect your bluetooth headphones to the phone.第 3 步:现在将蓝牙耳机连接到手机。 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 // 步骤 4-5(可选):查看应用程序类别本身没有问题

Step 4: Now force kill the “BluetoothIssue” app, while your headphones are still connected.第 4 步:现在强制关闭“BluetoothIssue”应用程序,同时您的耳机仍处于连接状态。

Step 5: Now relaunch the “BluetoothIssue” app.第 5 步:现在重新启动“BluetoothIssue”应用程序。 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).我了解到,如果我将模式设置为 VideoRecording,如果我打开蓝牙,iOS 将更改路由(并且我收到路由更改通知)。 Without this mode I had to allow both bluetooth, after detect one, change the category again without AllowBluetooth, just A2DP.如果没有这种模式,我必须同时允许两个蓝牙,在检测到一个之后,在没有 AllowBluetooth 的情况下再次更改类别,只是 A2DP。 Weird and slow solution, but worked.奇怪而缓慢的解决方案,但有效。 Now im just using videorecording mode.现在我只是使用录像模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以编程方式扫描并连接到iOS上的Bluetooth A2DP设备 - How to scan and connect to Bluetooth A2DP device on iOS programmatically 具有A2DP输出通道的AVAudioSession的降低延迟 - Deacrease latency for AVAudioSession with A2DP output channel AVAudioSession 无法识别来自蓝牙设备的音频 - AVAudioSession does not recognise audio from bluetooth device iOS上的蓝牙A2DP API支持? - Bluetooth A2DP API Support on iOS? iOS蓝牙双模; 同时将BLE(GATT)连接到已连接的BR / EDR(A2DP / HFP)立体声耳机 - iOS Bluetooth dual-mode; connect BLE (GATT) to an already connected BR/EDR (A2DP/HFP) stereo headset simultaneously 是否可以将十六进制数据快速发送到 A2DP 蓝牙设备? - Is possible send hexadecimal data to A2DP Bluetooth Device in swift? 如何在iPhone上使用A2DP连接到远程设备 - How to connect to a remote device using A2DP on iPhone iOS 7使用AVAudioSession无法通过蓝牙接收器获得音频 - iOS 7 Can't have audio via bluetooth receiver using AVAudioSession 如何使用蓝牙4.0(BLE)如何使A2DP蓝牙配置文件工作? 我需要AptX编解码器吗? - How are you able to get an A2DP bluetooth profile to work using bluetooth 4.0 (BLE)? Do I need an AptX codec? 在 AVAudioSession 中使用蓝牙播放 - Playing using bluetooth in AVAudioSession
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM