Something is wrong here. The code, as you show it, could not even compile. You cannot append
string[]
to a grammar builder, you have to append an instance of
Choice
. This is a correct code fragment:
string[] phrases = new string[] {
}
Choices choiceSet = new Choices(phrases);
GrammarBuilder grammarBuilder = new GrammarBuilder();
grammarBuilder.Append(choiceSet);
or, even simpler
string[] phrases = new string[] {
}
Choices choiceSet = new Choices(phrases);
GrammarBuilder grammarBuilder = new GrammarBuilder(choiceSet);
In addition to that (sigh)… there is a very, very subliminal problem related, surprisingly, to apartment model you are using with the recognizer. There are two different engines. Depending on the platform and probably .NET version, the engine
System.Speech.Recognition.SpeechRecognitionEngine
requires [MTAThread], another one
System.Speech.Recognition.SpeechRecognizer
, requries [STAThread].
Wrong choice of the apartment state, by some reason, cause the message you reported. A while ago, I quickly figured it out, because rudimentary Microsoft samples worked and could be used for comparison. However, this problem has gone when I switched from XP to Windows 7, where both engines works with either of the two apartment states. Not all application types allow to chose any apartment state for a main thread, but you can always do all your work in some other thread you can create with required apartment state.
So, first, follow my code fragment above and, if it does not yet work, try to change apartment state. Before you ask some follow up questions, please try it out and then tell us you OS version, .NET version and type of the engine.
By the way, this answer is based on testing I've just done. All works. If you want, I'll give you a complete sample, but after your try to fix your code, please.
[EDIT]
As I promised, this is a fully functional sample:
namespace SpeechConsole {
using System;
using System.Speech.Recognition;
class Program {
const string QuiteCommand = "Quit";
const string RepeatInstructionCommand = "Repeat instructions";
const string ShutUpCommand = "Shut up";
void Run() {
try {
string[] phrases = new string[] {
"10", "Previous", "Remember", "Forget", "Save",
RepeatInstructionCommand, ShutUpCommand, QuiteCommand, };
Choices choiceSet = new Choices(phrases);
GrammarBuilder grammarBuilder = new GrammarBuilder(choiceSet);
grammarBuilder.Append(choiceSet);
SpeechRecognitionEngine engine = new SpeechRecognitionEngine();
engine.LoadGrammar(new Grammar(grammarBuilder));
engine.RecognizeCompleted += (source, eventInfo) => {
};
engine.SpeechRecognitionRejected += (source, eventInfo) => {
Console.WriteLine("What the hell you're trying to say?!");
};
engine.SpeechRecognized += (source, eventInfo) => {
Console.WriteLine(@"Detected: ""{0}""", eventInfo.Result.Text);
if (eventInfo.Result.Text == QuiteCommand)
System.Diagnostics.Process.GetCurrentProcess().Kill();
if (eventInfo.Result.Text == RepeatInstructionCommand)
Instructions(phrases);
};
engine.SetInputToDefaultAudioDevice();
Instructions(phrases);
engine.RecognizeAsync(RecognizeMode.Multiple);
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
} catch (Exception e) {
Console.WriteLine("{0}: {1}", e.GetType().Name, e.Message);
}
}
void Instructions(string[] phrases) {
Console.WriteLine("Say one of the following:");
foreach (string prompt in phrases)
Console.WriteLine(" " + prompt);
Console.WriteLine();
}
static void Main() {
Console.Title = "Ctrl+C or say \"Quit\" to exit";
new Program().Run();
}
}
}
—SA