2017-09-24 74 views
3

我想创建一个语音到文本的功能,我得到的错误:(SWIFT)初始值设定条件的结合必须有可选的类型,而不是“AVAudioInputNode”

Initializer for conditional binding must have Optional type, not 'AVAudioInputNode'

guard let inputNode = audioEngine.inputNode else { 
     fatalError("Audio engine has no input node") 
    } 
+0

我有完全相反的问题,在我的情况下'inputNode'即使文档中另有说明,也是可选的。 – shelll

+0

你是如何实现的,它不是零?你使用Swift 4吗?你链接到你的项目的哪个“AV”框架? – shelll

+0

这是Apple的示例代码,它看起来不起作用 – user798719

回答

0

AVAudioEngine“ s inputNode属性不是可选的。当第一次访问inputNode时,音频引擎按需创建单例。它不可能是零,因为这个守卫没有意义。

因此,只需移除警卫并按原样使用audioEngine.inputNode即可。它不能是nil

你仍然需要确保该inputNode连接到的东西在使用它之前:

Check the input format of input node (specifically, the hardware format) for a non-zero sample rate and channel count to see if input is enabled.

(从苹果公司的文档)

+0

频率或通道数为零时我们该怎么办?我遇到这种情况几次,只有设备重新启动修复它... – shelll

+0

任何人都可以解决这个问题@shelll提到了什么? – Tarun

相关问题