How do I load a text grammar in a SAPI 5.4 C# program?
I've been using SRGS grammars with SAPI 5.4 to de开发者_Go百科fine command and control grammars. Now I'd like to switch to text grammars so they'd be compatible with SAPI 5.1 (and possibly even SAPI 4?).
I found an example of a SAPI 5.4 compatible text grammar here
I'd like to load that grammar from it's XML file at runtime as opposed to using the grammar compiler tool and then including the binary version of the grammar as a resource. Ideally I'd like to be able to create an XDocument in memory and then have the Speech.Recognizer listen for those commands.
Does anyone know how to do this? Thanks!
To use the text grammars you have to directly interact with the SAPI COM (by including the Microsoft Speech Object Library) - instead of just using the higher level System.Speech.
So if you copied the text grammar linked to above and saved it as sol.xml (which it is in the SAPI SDK samples) you could load it using the following code:
SpSharedRecoContext reco = new SpSharedRecoContext();
ISpeechRecoGrammar grammar;
grammar = reco.CreateGrammar();
grammar.CmdLoadFromFile("sol.xml");
grammar.CmdSetRuleIdState(0, SpeechRuleState.SGDSActive);
I tested that code with XP (5.1), Visa (5.3), and 7 (5.4).
精彩评论