【发布时间】: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