【问题标题】:The delegate function is broken in this code. What should I do to fix it?委托功能在此代码中被破坏。我应该怎么做才能修复它?
【发布时间】:2019-08-31 00:02:43
【问题描述】:

我无法让这段代码在 Xcode 中工作。我什至将 plist 设置为与麦克风使用和语音识别使用一起使用,但在清理并使用运行构建后仍然无法使用。

我查看了很多示例,所有示例都以完全相同的方式进行。

    import Cocoa  
    import Foundation  
    import AppKit  

    class ViewController: NSViewController, NSSpeechRecognizerDelegate {  

        var speechRec: NSSpeechRecognizer = NSSpeechRecognizer()!  
        var commands = ["a","b","c","d","stop","go"]  

        override func viewDidLoad() {  
            super.viewDidLoad()  
            speechRec.commands = commands  
            speechRec.delegate = self  
            //speechRec.listensInForegroundOnly = true  
            //speechRec.blocksOtherRecognizers = true  
            speechRec.startListening()  
        }  

        override var representedObject: Any? {  
            didSet {  
            // Update the view, if already loaded.  
            }  
        }  

        @IBAction func Listen(_ sender: Any) {  
            speechRec.startListening()  
            print("Listening now...")  
        }  

        @IBAction func Stop(_ sender: Any) {  
            speechRec.stopListening()  
            print("Stopped Listening...")  
        }  

        func speechRecognizer(_ sender: NSSpeechRecognizer, didRecognizeCommand command: String) {  

            print("Recognized...")  

            if (command as String == "a") {  
                print("You picked \(command)")              // Debug print  
            }  
            else if (command as String == "b") {  
                print("You picked \(command)")              // Debug print  
            }  
            else if (command as String == "c") {  
                print("You picked \(command)")              // Debug print  
            }  
            else if (command as String == "d") {  
                print("You picked \(command)")              // Debug print  
            }  
            else if (command as String == "stop") {  
                print("You picked \(command)")              // Debug print  
            }  
            else if (command as String == "go") {  
                print("You picked \(command)")              // Debug print  
            }  
            else {  
                print("You picked \(command)")  
            }  
        }  
    }  


【问题讨论】:

    标签: swift speech-recognition appkit


    【解决方案1】:

    我能够在一个空的 Cocoa 项目中成功使用您的代码来检测语音。它构建并成功运行,并在检测到语音命令时调用了委托方法。

    • 要使用 AppKit 语音库,我必须下载一个 1GB 的 SpeechKit 库(当我第一次尝试运行该应用程序时,XCode 10.2.1 提示我)。您是否下载了所需的库?

    • 您的麦克风是否正确配置为“系统偏好设置”的“声音设置”面板中的“输入”源?

    • 关闭应用沙盒允许直接访问麦克风。关掉App Sandbox还能用吗?

    【讨论】:

      猜你喜欢
      • 2022-11-10
      • 1970-01-01
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      • 2022-11-24
      • 1970-01-01
      • 2018-06-28
      • 2019-01-06
      相关资源
      最近更新 更多