2017-09-24 81 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

相關問題