2012-11-28 4 views
1

저는 프로그래밍 초보자입니다. 음성 인식을 사용하여 말하려고했던 것을 보여주는 메시지 상자를 표시하는 간단한 응용 프로그램을 작성하려고합니다. 문제는 처음으로 "hello"라고 말하면서 예를 들어 메시지 상자가 표시되지 않는 경우입니다. 한 번 더 시도하면 올바른 메시지 상자가 재생됩니다. "hello"라고 세 번째로 말하면 2 개의 메시지 상자가 표시됩니다. 네 번째 시간에는 3 개의 메시지 상자 등이 있습니다. 누구든지이 문제를 도울 수 있습니까?이슈 음성 인식 C#

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Speech.Recognition; 

namespace Voices 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private SpeechRecognitionEngine sre; 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      sre = new SpeechRecognitionEngine(); 
      sre.SetInputToDefaultAudioDevice(); 

      Choices commands = new Choices(); 
      commands.Add(new string[] { "hello" }); 

      GrammarBuilder gb = new GrammarBuilder(); 
      gb.Append(commands); 

      Grammar g = new Grammar(gb); 
      sre.LoadGrammar(g); 

      sre.RecognizeAsync(RecognizeMode.Multiple); 


      sre.SpeechRecognized += (s, args) => 
      { 
       foreach (RecognizedPhrase phrase in args.Result.Alternates) 
       { 
        if (phrase.Confidence > 0.9f) 
         sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized); 
       } 
      }; 

     } 

     void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) 
     { 
      switch (e.Result.Text) 
      { 
       case "hello": 
        MessageBox.Show(e.Result.Text);      
        break; 
      } 
     }  
    } 
} 
+0

당신이 쓰는 왜'foreach' 루프 :

나는 싶은 것은 다음과 같은 것 같아요? – SLaks

답변

3

다음 코드는 여러 개의 메시지 상자를 얻을 이유입니다.

이벤트에 한 번만 등록해야합니다.

if (phrase.Confidence > 0.9f) 
    sre_SpeechRecognized(s, args); 
+0

효과가있었습니다! 대단히 감사합니다 !!! – user1840006

4

사용자가 말할 때마다 인라인 이벤트 처리기()에 새 이벤트 처리기가 추가되고 있습니다. 이 같은 이벤트 핸들러와 같은 이벤트에 등록하고, 을 SpeechRecognized

sre.SpeechRecognized += (s, args) => 
{ 
    foreach (RecognizedPhrase phrase in args.Result.Alternates) 
    { 
     if (phrase.Confidence > 0.9f) 
      sre.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(sre_SpeechRecognized); 
    } 
}; 

매번 발생 :

+0

도움 주셔서 감사합니다 !! – user1840006