我在尝试实施 Microsoft 提供的有关如何使用 SpeechRecognitionEngine 的示例时遇到重大问题(https://msdn.microsoft.com/en-us/library/system.speech.recognition.speechrecognitionengine.speechdetected(v=vs. 110).aspx )。
这是示例代码:
using System;
using System.Speech.Recognition;
namespace SampleRecognition
{
class Program
{
static void Main(string[] args)
// Initialize an in-process speech recognition engine.
{
using (SpeechRecognitionEngine recognizer =
new SpeechRecognitionEngine())
{
// Create a grammar.
Choices cities = new Choices(new string[] {
"Los Angeles", "New York", "Chicago", "San Francisco", "Miami", "Dallas" });
GrammarBuilder gb = new GrammarBuilder();
gb.Culture = new System.Globalization.CultureInfo("en-GB");
gb.Append("I would like to fly from");
gb.Append(cities);
gb.Append("to");
gb.Append(cities);
// Create a Grammar object and load it to the recognizer.
Grammar g = new Grammar(gb);
g.Name = ("City Chooser");
recognizer.LoadGrammarAsync(g);
// Attach event handlers.
recognizer.LoadGrammarCompleted +=
new EventHandler<LoadGrammarCompletedEventArgs>(recognizer_LoadGrammarCompleted);
recognizer.SpeechDetected +=
new EventHandler<SpeechDetectedEventArgs>(recognizer_SpeechDetected);
recognizer.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
// Set the input to the recognizer.
recognizer.SetInputToDefaultAudioDevice();
// Start recognition.
recognizer.RecognizeAsync();
// Keep the console window open.
Console.ReadLine();
}
}
// Handle the SpeechDetected event.
static void recognizer_SpeechDetected(object sender, SpeechDetectedEventArgs e)
{
Console.WriteLine(" Speech detected at AudioPosition = {0}", e.AudioPosition);
}
// Handle the LoadGrammarCompleted event.
static void recognizer_LoadGrammarCompleted(object sender, LoadGrammarCompletedEventArgs e)
{
Console.WriteLine("Grammar loaded: " + e.Grammar.Name);
}
// Handle the SpeechRecognized event.
static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine(" Speech recognized: " + e.Result.Text);
}
}
}
似乎没有任何效果,我尝试了许多不同的示例,并花了一整天的时间试图让它发挥作用。例如,如果我将RecognizeAsync()换成EmulateRecognizeAsync ("I would like to fly from Chicago to Miami"),它会按预期工作。但似乎该程序没有从我的麦克风中获得任何输入。
以下是更多细节:
- 视窗 8.1
- .NET 4.0
- 联想 ThinkPad 内置麦克风
- 视觉工作室 2012
值得一提的是,程序的输出中包含在加载语法时会写出的以下信息消息:
ConsoleApplication1.vshost.exe Information: 0 : SAPI does not implement phonetic alphabet selection.
我错过了什么吗?我需要更好的麦克风吗?除了将麦克风设为默认麦克风之外,我还需要设置硬件吗?我需要为 Windows 8 使用不同的库吗?我没主意了。
提前谢谢你!