为什么我在windows7系统中使用SpeechLib.SpSharedRecoContext时,会自动打开语音识别工具系统自带的?

时间:2013-11-21 15:38:03

标签: sapi

为什么我在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系统中,这不是现象。

1 个答案:

答案 0 :(得分:0)

在Windows Vista&amp;上面,创建共享识别器(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();
}