我正在尝试录制和语音识别通过我的蓝牙AirPods的麦克风传来的音频 .
我尝试了一切,但没有运气 . 我能够从内置麦克风录制,但是一旦我将音频类别设置为蓝牙,它就会崩溃 .
这是我的代码的当前版本:
askSpeechPermission()
var request = SFSpeechAudioBufferRecognitionRequest()
var listOfInputs = AVAudioSession.sharedInstance().availableInputs
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryRecord, mode: AVAudioSessionModeDefault, options: AVAudioSessionCategoryOptions.allowBluetooth)
} catch {
}
let node = audioEngine.inputNode
let recordingFormat = node.outputFormat(forBus: 0)
node.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { buffer, _ in
self.request.append(buffer)
}
这是导致的崩溃错误 .
***因未捕获的异常'com.apple.coreaudio.avfaudio'而终止应用程序,原因:'必需条件为false:format.sampleRate == hwFormat.sampleRate'
2 回答
你试过检查你的采样率吗?要修改您的采样率,您可以使用
然后你可以使用你的格式
我有同样的问题,我解决了添加BluetoothA2DP选项:
audioSession = [AVAudioSession sharedInstance]; [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord withOptions:(AVAudioSessionCategoryOptionAllowBluetooth | AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionAllowBluetoothA2DP)error:nil];