如何从iOS设备获取硬件采样率?



在我的应用程序中,我正在使用AVAudioEngine进行语音识别,但我很难理解如何正确设置inputNode。

我使用本教程作为我的代码示例:https://www.raywenderlich.com/155752/speech-recognition-tutorial-ios

这部分代码让我头疼:

let node = audioEngine.inputNode
let recordingFormat = node.outputFormat(forBus: 0)
node.installTap(onBus: 0, bufferSize: 1024,
format: recordingFormat) { [unowned self]
(buffer, _) in
self.request.append(buffer)
}
audioEngine.prepare()
try audioEngine.start()

基本上,我的应用程序崩溃并出现此错误:

AVAEInternal.h:70:_AVAE_Check: required condition is false: [AVAudioIONodeImpl.mm:897:SetOutputFormat:
(format.sampleRate == hwFormat.sampleRate)]

所以我的问题是,如何从运行应用程序的设备获取硬件采样率,以便我可以将其设置为节点的录制格式?

此外,如果正在运行的应用程序是通过 mac 录制或通过 apple TV 进行屏幕共享的,我认为我需要获取这些设备的硬件采样率,这样我的应用程序在这些情况下也不会崩溃?!

非常感谢任何形式的帮助!

来自AVAudioSession的硬件采样率。

let sampleRate = AVAudioSession.sharedInstance().sampleRate

但这可能更适合您的具体情况。

let sampleRate = node.inputFormat(forBus: 0).sampleRate

最新更新