开发者

Doubt with c# handlers?

I 开发者_运维百科have this code in c#

public void startRecognition(string pName)
    {
        presentationName = pName;

        if (WaveNative.waveInGetNumDevs() > 0)
        {
            string grammar = System.Environment.GetEnvironmentVariable("PUBLIC") + "\\SoundLog\\Presentations\\" + presentationName + "\\SpeechRecognition\\soundlog.cfg";

/*                if (File.Exists(grammar))
                {
                    File.Delete(grammar);
                }
                executeCommand();*/
            recContext = new SpSharedRecoContextClass();
            recContext.CreateGrammar(0, out recGrammar);

            if (File.Exists(grammar))
            {
                recGrammar.LoadCmdFromFile(grammar, SPLOADOPTIONS.SPLO_STATIC);
                recGrammar.SetGrammarState(SPGRAMMARSTATE.SPGS_ENABLED);
                recGrammar.SetRuleIdState(0, SPRULESTATE.SPRS_ACTIVE);
            }

            recContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(handleRecognition);
            //recContext.RecognitionForOtherContext += new _ISpeechRecoContextEvents_RecognitionForOtherContextEventHandler(handleRecognition);

            //System.Windows.Forms.MessageBox.Show("olari");


        }
    }

    private void handleRecognition(int StreamNumber,
        object StreamPosition,
        SpeechLib.SpeechRecognitionType RecognitionType,
        SpeechLib.ISpeechRecoResult Result)
    {
        System.Windows.Forms.MessageBox.Show("entrei");

        string temp = Result.PhraseInfo.GetText(0, -1, true);
        _recognizedText = "";

        foreach (string word in recognizedWords)
        {
            if (temp.Contains(word))
            {
                _recognizedText = word;
            }
        }
    }





public void run()
    {
        if (File.Exists(System.Environment.GetEnvironmentVariable("PUBLIC") + 

"\\SoundLog\\Serialization\\Voices\\identifiedVoicesDLL.txt"))
    {
        deserializer = new XmlSerializer(_identifiedVoices.GetType());
        FileStream fs = new FileStream(System.Environment.GetEnvironmentVariable("PUBLIC") + "\\SoundLog\\Serialization\\Voices\\identifiedVoicesDLL.txt", FileMode.Open);
        Object o = deserializer.Deserialize(fs);
        fs.Close();
        _identifiedVoices = (double[])o;
    }


    if (File.Exists(System.Environment.GetEnvironmentVariable("PUBLIC") + "\\SoundLog\\Serialization\\Voices\\deletedVoicesDLL.txt"))
    {
        deserializer = new XmlSerializer(_deletedVoices.GetType());
        FileStream fs = new FileStream(System.Environment.GetEnvironmentVariable("PUBLIC") + "\\SoundLog\\Serialization\\Voices\\deletedVoicesDLL.txt", FileMode.Open);
        Object o = deserializer.Deserialize(fs);
        fs.Close();
        _deletedVoices = (ArrayList)o;
    }

    myTimer.Interval = 5000;
    myTimer.Tick += new EventHandler(clearData);
    myTimer.Start();
    if (WaveNative.waveInGetNumDevs() > 0) {
        _waveFormat = new WaveFormat(_samples, 16, 2);
        _recorder = new WaveInRecorder(-1, _waveFormat, 8192 * 2, 3, new BufferDoneEventHandler(DataArrived));
        _scaleHz = (double)_samples / _fftLength;
        _limit = (int)((double)_limitVoice / _scaleHz);
        SoundLogDLL.MelFrequencyCepstrumCoefficients.calculateFrequencies(_samples, _fftLength);
    }
}

startRecognition is a method for Speech Recognition that load a grammar and makes the recognition handler here:

recContext.Recognition += new _ISpeechRecoContextEvents_RecognitionEventHandler(handleRecognition);

Now I have a problem, when I call the method startRecognition before method run, both handlers (the recognition one and the handler for the Tick) work well. If a word is recognized, handlerRecognition method is called.

But, when I call the method run before the method startRecognition, both methods seem to run well but then the recognition Handler is never executed! Even when I see that words are recognized (because they happear on the Windows Speech Recognition app).

What can I do for the recognition handler be allways called?


If you don't call StartRecognition before Run, the handler never gets hooked up. So the obvious solution would be to make sure that your line of code that hooks up the recognition handler gets called.

The simpliest solution is the best one :)

I made the recognition handler in the run() method.

I call the startReconition method method after run and it worked!

thanks Robert Harvey

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜