简体   繁体   中英

C# speech recognition (System.Speech.Recognition) issues

I've been working with a bit of speech recognition for a few days with various test programs and it's all worked fine. However i've tried implementing it into my OpenGL project and the function 'Recognized' is now not being called.

Up in the Windows Speech Recognition thing (the thing that says "try saying 'Start Listening'" an awful lot), words that are loaded appear when i say them, so I am assuming that it is correctly detecting words, it's just for some reason not triggering the event.

Here's the code i've been using. All you really need to know (besides what is shown in the code), is that AddCommands is called somewhere else, to add in a few words that i've been testing with and that 'Initiate' is called upon the loading of the form.

public class SpeechControls
{
    public static SpeechRecognizer sRecognizer;

    private static Dictionary<string, IVoiceControlable> controllers = new Dictionary<string, IVoiceControlable>();

    public static void Initiate()
    {
        sRecognizer = new SpeechRecognizer();
        sRecognizer.Enabled = true;

        sRecognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(Recognized);
    }

    private static void Recognized(object obj, SpeechRecognizedEventArgs args)
    {
        controllers[args.Result.Text].TriggerCommand(args.Result.Text);
    }

    public static void AddCommands(string[] commands, IVoiceControlable control)
    {
        foreach (string str in commands)
        {
            controllers.Add(str, control);
        }

        sRecognizer.LoadGrammar(new Grammar(new GrammarBuilder(new Choices(commands))));
    }
}

Does anyone know why 'Recognized' would not be triggered?

Thanks for any help, much appreciated.

Because OpenGL runs of game loops rather than event listening, the thread is completely taken up by the loop. To start listening for commands a second thread is needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM