lit*_*ium 6 audio avfoundation ios avaudiosession swift
我的应用程序似乎无法与 AirPods 配合使用。现在我正在使用此代码进行播放和录制:
\n\n let audioSession = AVAudioSession.sharedInstance()\n do { try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.defaultToSpeaker)\n }catch {\n print("audioSession properties weren\'t set because of an error.")\n }\nRun Code Online (Sandbox Code Playgroud)\n\ndefaultToSpeaker如果我改为就够了吗allowBluetooth?
PS 我知道这是一个相当愚蠢的问题,因为只需更改此行并检查会简单得多,但我现在没有 AirPods,所以我唯一的选择就是上传Testflight 的新构建(我想以最少的迭代来完成此操作)。
\n\n更新:(相当幼稚的方法 \xe2\x80\x94 但我需要的只是使用蓝牙耳机(如果可用):
\n\nfunc selectDevice(audioSession: AVAudioSession) {\n\n var headphonesExist = false\n var bluetoothExist = false\n var speakerExist = false\n\n let currentRoute = AVAudioSession.sharedInstance().currentRoute\n\n for output in audioSession.currentRoute.outputs {\n print(output)\n\n if output.portType == AVAudioSessionPortHeadphones || output.portType == AVAudioSessionPortHeadsetMic {\n headphonesExist = true\n }\n\n if output.portType == AVAudioSessionPortBluetoothA2DP || output.portType == AVAudioSessionPortBluetoothHFP {\n bluetoothExist = true\n }\n\n if output.portType == AVAudioSessionPortBuiltInSpeaker {\n speakerExist = true\n }\n }\n\n if bluetoothExist == true {\n do { try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord, with: AVAudioSessionCategoryOptions.allowBluetooth) } catch {\n print("error with audiosession: bluetooth")\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
小智 6
您需要将蓝牙支持添加到您的options参数中,如下所示:
[AVAudioSessionCategoryOptions.defaultToSpeaker, .allowBluetoothA2DP]
.allowBluetoothA2DP将允许向蓝牙设备输出高质量音频并限制该设备上的麦克风输入,同时.allowBluetooth将默认 HFP 兼容(输入/输出)蓝牙设备设置为支持麦克风输入的较低质量 HFP。
| 归档时间: |
|
| 查看次数: |
4331 次 |
| 最近记录: |