控制台语音识别在启动后立即退出
Console Speech Recognition exits right after start
我尝试按照 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;
}
}
}
}
并使用单声道项目编译如下:
c:\mcs /reference:System.Speech.dll Program.cs
将 System.Speech.dll
添加到文件夹项目后,生成了 Program.exe
文件。
我在终端运行program
后,直接结束,不给我说什么的机会!!
我有 2 个问题:
我在这里遗漏了什么,我做错了什么?
和
如何以更好的方式添加“.dll”文件,我尝试如下将其添加到 Project.json
文件中,但没有成功,尽管我没有收到任何错误在 运行宁 dotnet restore
:
"frameworks": {
"netcoreapp1.0": {
"bin": {
"assembly": "D:/2016/Speech/CORE/System.Speech.dll"
},
}
}
我通过添加 Thread.Sleep
解决了第一部分,因此它为线程提供了足够的时间继续监听,另一种选择是使用 while(true);
使其无限循环
我仍然无法解决第二部分,即如何使 VS 代码识别程序集文件的存在。
新的完整代码,如果有兴趣在下面,可以找到更全面的代码here:
using System;
using System.Speech.Recognition;
using System.Threading;
//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.SpeechRecognized += recEngine_SpeechRecognized;
Console.WriteLine("Starting asynchronous recognition...");
recEngine.RecognizeAsync(RecognizeMode.Multiple);
// Wait 30 seconds, and then cancel asynchronous recognition.
Thread.Sleep(TimeSpan.FromSeconds(30));
// or
// while(true);
}
// 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 "say Hello":
Console.WriteLine("you said hi");
break;
default:
break;
}
}
}
}
我尝试按照 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;
}
}
}
}
并使用单声道项目编译如下:
c:\mcs /reference:System.Speech.dll Program.cs
将 System.Speech.dll
添加到文件夹项目后,生成了 Program.exe
文件。
我在终端运行program
后,直接结束,不给我说什么的机会!!
我有 2 个问题:
我在这里遗漏了什么,我做错了什么?
和
如何以更好的方式添加“.dll”文件,我尝试如下将其添加到 Project.json
文件中,但没有成功,尽管我没有收到任何错误在 运行宁 dotnet restore
:
"frameworks": {
"netcoreapp1.0": {
"bin": {
"assembly": "D:/2016/Speech/CORE/System.Speech.dll"
},
}
}
我通过添加 Thread.Sleep
解决了第一部分,因此它为线程提供了足够的时间继续监听,另一种选择是使用 while(true);
我仍然无法解决第二部分,即如何使 VS 代码识别程序集文件的存在。
新的完整代码,如果有兴趣在下面,可以找到更全面的代码here:
using System;
using System.Speech.Recognition;
using System.Threading;
//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.SpeechRecognized += recEngine_SpeechRecognized;
Console.WriteLine("Starting asynchronous recognition...");
recEngine.RecognizeAsync(RecognizeMode.Multiple);
// Wait 30 seconds, and then cancel asynchronous recognition.
Thread.Sleep(TimeSpan.FromSeconds(30));
// or
// while(true);
}
// 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 "say Hello":
Console.WriteLine("you said hi");
break;
default:
break;
}
}
}
}