问题
Why when I use SpeechLib.SpSharedRecoContext in the windows7 system, will automatically open the speech recognition tool system comes with? The following is my code,when it running the speech recognition tool in windows7 system will open, and I must click the system tools begin button,then my program can begin recognize.
private const int grammarId = 10;
private bool speechInitialized = false;
private SpeechLib.SpSharedRecoContext objRecoContext;
private SpeechLib.ISpeechRecoGrammar grammar;
private SpeechLib.ISpeechGrammarRule ruleListItems;
private void InitializeSpeech(List<string> userKeyWords = null, bool isUseSystemGrammar = false)
{
try
{
// First of all, let's create the main reco context object.
// In this sample, we are using shared reco context. Inproc reco
// context is also available. Please see the document to decide
// which is best for your application.
objRecoContext = new SpeechLib.SpSharedRecoContext();
// Then, let's set up the event handler. We only care about
// Hypothesis and Recognition events in this sample.
objRecoContext.Hypothesis += new
_ISpeechRecoContextEvents_HypothesisEventHandler(
RecoContext_Hypothesis);
objRecoContext.Recognition += new
_ISpeechRecoContextEvents_RecognitionEventHandler(
RecoContext_Recognition);
objRecoContext.AudioLevel += new _ISpeechRecoContextEvents_AudioLevelEventHandler(objRecoContext_AudioLevel);
objRecoContext.EventInterests = SpeechRecoEvents.SREAllEvents;
// Now let's build the grammar.
grammar = objRecoContext.CreateGrammar(grammarId);
ruleListItems = grammar.Rules.Add("ListItemsRule",
SpeechRuleAttributes.SRATopLevel | SpeechRuleAttributes.SRADynamic, 1);
RebuildGrammar(userKeyWords, isUseSystemGrammar);
// Now we can activate the top level rule. In this sample, only
// the top level rule needs to activated. The ListItemsRule is
// referenced by the top level rule.
grammar.CmdSetRuleState("ListItemsRule", SpeechRuleState.SGDSActive);
speechInitialized = true;
}
catch (Exception e)
{
Loger.LogErr(e);
}
}
How can I prevent my program dependence on system tool?Thank you.
By the way,in windows xp system,this is not the phenomenon.
回答1:
In Windows Vista & above, creating a shared recognizer (SpeechLib.SpSharedRecoContext
) will automatically start Windows Speech Recognition, which serves as the common UI for shared recognizers.
If you don't want that, you can create an in-process recognizer (SpeechLib.SpInProcRecoContext
) - however, if you do this, you need to explicitly manage the audio source, recognition engine, and user profile:
private SpeechLib.SpInprocRecoContext CreateInprocRecoContext()
{
SpeechLib.SpObjectTokenCategory Category = new Speechlib.SpObjectTokenCategory();
Category.SetId(SpeechLib.SpeechStringConstants.SpeechCategoryAudioIn);
SpeechLib.SpObjectToken AudioToken = new SpeechLib.SpObjectToken();
AudioToken.SetId(Category.Default());
Category.SetId(SpeechLib.SpeechStringConstants.SpeechCategoryRecognizers);
SpeechLib.SpObjectToken EngineToken = new SpeechLib.SpObjectToken();
EngineToken.SetId(Category.Default());
Category.SetId(SpeechLib.SpeechStringConstants.SpeechCategoryRecoProfiles);
SpeechLib.SpObjectToken ProfileToken = new SpeechLib.SpObjectToken();
ProfileToken.SetId(Category.Default());
SpeechLib.SpInprocRecognizer reco = new SpeechLib.SpInprocRecognizer();
reco.SetRecognizer(EngineToken);
reco.SetInput(AudioToken);
reco.SetRecoProfile(ProfileToken);
return reco.CreateRecoContext();
}
来源:https://stackoverflow.com/questions/20125238/why-when-i-use-speechlib-spsharedrecocontext-in-the-windows7-system-will-automa