【发布时间】:2017-01-23 14:51:26
【问题描述】:
我尝试按照 this 教程构建语音识别 C# 应用程序,唯一的区别是我想要一个 Console 应用程序,而不是 Win Form 应用程序,所以我写了这段代码:
using System;
using System.Speech.Recognition;
//using System.Speech.Synthesis;
namespace Voice_Recognation
{
class Program
{
static void Main(string[] args)
{
SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine();
recEngine.SetInputToDefaultAudioDevice();
Choices commands = new Choices();
commands.Add(new string[] { "say Hi", "say Hello"});
GrammarBuilder gb = new GrammarBuilder();
gb.Append(commands);
Grammar g = new Grammar(gb);
recEngine.LoadGrammarAsync(g);
recEngine.RecognizeAsync(RecognizeMode.Multiple);
recEngine.SpeechRecognized += recEngine_SpeechRecognized;
}
// Create a simple handler for the SpeechRecognized event
static void recEngine_SpeechRecognized (object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine("Speech recognized: {0}", e.Result.Text);
switch(e.Result.Text){
case "Red":
Console.WriteLine("you said hi");
break;
default:
break;
}
}
}
}
并使用mono项目编译如下:
c:\mcs /reference:System.Speech.dll Program.cs
将System.Speech.dll添加到文件夹项目后,生成Program.exe文件。
一旦我在终端运行program,它就直接结束了,没有给我任何机会说什么!!
我有 2 个问题:
我在这里遗漏了什么,我做错了什么?
和
有没有办法以更好的方式添加“.dll”文件,我尝试将其添加到Project.json 文件中,如下所示,但没有奏效,虽然我在运行@987654332 时没有遇到任何错误@:
"frameworks": {
"netcoreapp1.0": {
"bin": {
"assembly": "D:/2016/Speech/CORE/System.Speech.dll"
},
}
}
【问题讨论】:
-
由于您使用的是异步方法,因此您需要等待一些事件以防止退出 main。例如,您可以使用 Console.ReadLine。
标签: c# mono .net-core voice-recognition