NSSpeechRecognition不响应macOS语音识别

时间:2018-08-02 05:37:49

标签: macos speech-recognition nsspeechrecognizer

我在这里是菜鸟,所以我可能缺少一些基本知识。在下面找到我的代码,我似乎无法弄清楚为什么不调用语音识别功能。麦克风处于活动状态,并且已填充字典,但未进行识别。有人可以帮我吗。

//
//  ViewController.swift
//  Kara
//
//  Created by Akash Sonthalia on 31/07/18.
//  Copyright © 2018 Akash Sonthalia. All rights reserved.
//

import Cocoa

class ViewController: NSViewController, NSSpeechRecognizerDelegate {

    var recognizer = NSSpeechRecognizer()

    @IBOutlet weak var karaStatus: NSTextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Kara's Ears 
        recognizer!.delegate = self
        recognizer!.commands = ["Hi", "Hello", "Kara Wake Up", "Kara Take a break"]
        recognizer!.startListening()
        recognizer!.blocksOtherRecognizers = true
        print("[Kara]: Has started listening")
    }

    func speechRecognizer(_ sender: NSSpeechRecognizer, didRecognizeCommand command: String) {
        print("************************")
        print(command)
        print("************************")
        self.karaStatus!.stringValue = "\(command)"
    }

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


}

控制台转储是以这种方式出现的,有人可以解释这意味着什么以及如何理解它们。

2018-08-02 10:58:03.476733+0530 Kara[13818:945857] Broker -> <dictionary: 0x604000163480> { count = 1, transaction: 0, voucher = 0x0, contents =
    "recID" => <int64: 0x6000002247c0>: 0
}
[Kara]: Has started listening
2018-08-02 10:58:13.920578+0530 Kara[13818:946011] Broker -> <dictionary: 0x6040001657c0> { count = 3, transaction: 1, voucher = 0x604000088700, contents =
    "blocked" => <bool: 0x7fffa713eb98>: true
    "msg" => <int64: 0x604000037520>: 600
    "recID" => <int64: 0x6040000372e0>: 0
}
2018-08-02 10:58:25.946379+0530 Kara[13818:946397] Broker -> <dictionary: 0x604000165700> { count = 3, transaction: 1, voucher = 0x604000083cf0, contents =
    "blocked" => <bool: 0x7fffa713ebb8>: false
    "msg" => <int64: 0x604000037440>: 600
    "recID" => <int64: 0x6040000348a0>: 0
}
2018-08-02 10:58:27.659137+0530 Kara[13818:946392] Broker -> <dictionary: 0x604000163780> { count = 3, transaction: 1, voucher = 0x604000086ae0, contents =
    "blocked" => <bool: 0x7fffa713eb98>: true
    "msg" => <int64: 0x604000037660>: 600
    "recID" => <int64: 0x604000036600>: 0
}
2018-08-02 11:00:31.255674+0530 Kara[13818:947580] Broker -> <dictionary: 0x604000165940> { count = 3, transaction: 1, voucher = 0x604000085c80, contents =
    "blocked" => <bool: 0x7fffa713ebb8>: false
    "msg" => <int64: 0x604000036600>: 600
    "recID" => <int64: 0x604000036ee0>: 0
}
2018-08-02 11:00:32.168927+0530 Kara[13818:947592] Broker -> <dictionary: 0x6000001633c0> { count = 3, transaction: 1, voucher = 0x6000000906d0, contents =
    "blocked" => <bool: 0x7fffa713eb98>: true
    "msg" => <int64: 0x60000003b540>: 600
    "recID" => <int64: 0x60000003b1e0>: 0
}
2018-08-02 11:03:11.842603+0530 Kara[13818:948910] Broker -> <dictionary: 0x6000001633c0> { count = 3, transaction: 1, voucher = 0x604000088160, contents =
    "blocked" => <bool: 0x7fffa713ebb8>: false
    "msg" => <int64: 0x60000003b540>: 600
    "recID" => <int64: 0x60000003b320>: 0
}
2018-08-02 11:03:12.690056+0530 Kara[13818:948910] Broker -> <dictionary: 0x604000165a00> { count = 3, transaction: 1, voucher = 0x604000083cf0, contents =
    "blocked" => <bool: 0x7fffa713eb98>: true
    "msg" => <int64: 0x6040000315c0>: 600
    "recID" => <int64: 0x604000037580>: 0
}

1 个答案:

答案 0 :(得分:1)

您的代码对我有用。我做了几件事:

  1. 在应用程序的应用程序沙箱设置中启用麦克风。

  2. 在“系统偏好设置”>“键盘”>“听写”中,有一个键盘快捷键设置。在运行您的应用程序后,我按下该快捷方式,听蜂鸣声,然后它识别出命令。

希望这会有所帮助。 :)