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

标签 ios swift avaudioengine

在我的应用程序中,我使用 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

关于ios - 如何从 iOS 设备获取硬件采样率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50712088/

相关文章:

ios - 如何在iOS Ionic应用程序中选择拨号器应用程序?

iphone - SQLite 表名无效字符

ios - 如何使用 UITableViewAutomaticDimension 更新单元格的高度?

swift - 自定义模式匹配失败,返回 `Enum case is not a member of type`

swift - 带有 AVAudioConverterInputBlock 的 AVAudioConverter 在处理后会出现断断续续的音频

ios - 自定义 UITableViewCell 内的 UIView 宽度/高度错误

ios - Xcode8 : Why Xcode8 is not building/compiling after converting to Swift3 syntax's

ios - Table view cell height based on Dynamic height Table Height问题

ios - 是否可以制作类型为 'aufc' (kAudioUnitType_FormatConverter) 的 AUv3 扩展?

swift - 在 Swift 中使用 AudioEngine 重复声音时崩溃