Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从AirPod麦克风录制音频_Ios_Swift - Fatal编程技术网

Ios 从AirPod麦克风录制音频

Ios 从AirPod麦克风录制音频,ios,swift,Ios,Swift,我正在尝试录制和识别通过蓝牙AirPods话筒发送的音频 我尝试了我发现的一切,但没有运气。我可以通过内置麦克风录音,但一旦我将音频类别设置为蓝牙,它就会崩溃 这是我的代码的当前版本: askSpeechPermission() var request = SFSpeechAudioBufferRecognitionRequest() var listOfInputs = AVAudioSession.sharedInstance().availableInputs do { tr

我正在尝试录制和识别通过蓝牙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”


你试过检查你的抽样率吗。要修改采样率,您可以使用

let fmt = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: 44100, channels: 1, interleaved: true)
然后你可以把它转换成你的格式

node.installTap(onBus: 0, bufferSize: 1024, format: fmt) { buffer, _ in
    self.request.append(buffer)
}

我遇到了同样的问题,我解决了添加BluetoothA2DP选项的问题:

audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord
              withOptions:(AVAudioSessionCategoryOptionAllowBluetooth | AVAudioSessionCategoryOptionDefaultToSpeaker | AVAudioSessionCategoryOptionAllowBluetoothA2DP)
                    error:nil];

按照奥斯曼的建议,我可以通过将采样率设置为AudioSession采样率来解决这个问题

let sampleRate = AVAudioSession.sharedInstance().sampleRate
let fmt = AVAudioFormat(commonFormat: .pcmFormatFloat32, sampleRate: sampleRate, channels: 1, interleaved: true)
node.installTap(onBus: 0, bufferSize: 1024, format: fmt) { buffer, _ in
    self.request.append(buffer)
}

希望这对某人有所帮助~

请添加“func”和您缺少的“{”……我尝试了您的建议,但它在installTap上不断崩溃,出现了相同的旧错误。