• 赚钱入口【需求资源】限时招募流量主、渠道主,站长合作;【合作模式】CPS长期分成,一次推广永久有收益。主动打款,不扣量;

如何使用AirPods的麦克风录音?

iOS cps12345 1周前 (11-17) 14次浏览 0个评论

目前的代码

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)
}

报crash

*** Terminating app due to uncaught exception ‘com.apple.coreaudio.avfaudio’, reason: ‘required condition is false: format.sampleRate == hwFormat.sampleRate’

解决

需要如下设置采样率,在最后添加如下代码

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)
}
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址