1

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

//
//  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
}
4

1 回答 1

2

你的代码对我有用。我做了一些额外的事情:

  1. 在您应用的应用沙盒设置中启用麦克风。

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

希望这可以帮助。:)

于 2018-08-08T16:42:32.833 回答