I am having major issues trying to implement a sample provided by Microsoft on how to use the SpeechRecognitionEngine (https://msdn.microsoft.com/en-us/library/system.speech.recognition.speechrecognitionengine.speechdetected(v=vs.110).aspx).
Here is the sample code:
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);
}
}
}
It seems like nothing works, I've tried many different examples and spent all day trying to get this to work. If I swap out RecognizeAsync() with EmulateRecognizeAsync("I would like to fly from Chicago to Miami"), for example, it works as expected. But it appears the program isn't getting any input from my mic.
Here are some more details:
- Windows 8.1
- .NET 4.0
- Lenovo ThinkPad built in microphone
- Visual Studios 2012
Worth mentioning that the output from the program has this info message that is written out when the grammar is loaded:
ConsoleApplication1.vshost.exe Information: 0 : SAPI does not implement phonetic alphabet selection.
Am I missing something? Do i need a better microphone? Do I need to setup up the hardware in anyway besides making the mic my default mic? Do i need to use a different library for Windows 8? I'm out of ideas.
Thank you in advanced!