为什么当我在windows7系统使用SpeechLib.SpSharedRecoContext,会自

2019-10-29 10:38发布

为什么当我在windows7系统使用SpeechLib.SpSharedRecoContext,会自动打开语音识别工具,系统自带的? 以下是我的代码,当它运行在Windows7系统中的语音识别工具将打开,我必须点击系统工具开始按钮,然后我的程序就可以开始认识。

 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);
        }
    }

如何防止我的系统工具程序的依赖?谢谢。

顺便说一句,在Windows XP系统中,这不是现象。

Answer 1:

在Windows Vista或以上,创建共享识别器( SpeechLib.SpSharedRecoContext )将自动启动Windows语音识别,其用作用于识别共享共用UI。

如果你不希望出现这种情况,你可以创建一个进程识别器( SpeechLib.SpInProcRecoContext ) -但是,如果你这样做,你需要明确地管理音频源,识别引擎和用户配置文件:

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();
}


文章来源: Why when I use SpeechLib.SpSharedRecoContext in the windows7 system, will automatically open the speech recognition tool system comes with?
标签: sapi