tan shen

tan shen

  • NA
  • 11
  • 1.9k

Why the speak appears before the switching of picture.

Nov 8 2017 8:21 PM
  1. SpeechSynthesizer ss = new SpeechSynthesizer();  
  2. PromptBuilder pb = new PromptBuilder();  
  3. SpeechRecognitionEngine sre = new SpeechRecognitionEngine();  
  4. Choices clist = new Choices();  
  5. int counter = 0;  
  6. int counter_CorrectAns = 0;   
  7. public void Question()  
  8. {  
  9. string path = @"C:\Users\Tan Wei Shen\Desktop\FYP\New folder\";  
  10. string[] filePaths = Directory.GetFiles(path, "*.jpg");  
  11. pictureBox1.Image = Image.FromFile(filePaths[counter]);  
  12. ss.SpeakAsync("What is this?");  
  13. clist.Add(new string[] { "apple","chicken","eraser" });  
  14. Grammar gr = new Grammar(new GrammarBuilder(clist));  
  15. try  
  16. {  
  17. sre.RequestRecognizerUpdate();  
  18. sre.LoadGrammar(gr);  
  19. sre.SpeechRecognized += sre_SpeechRecognized;  
  20. sre.SetInputToDefaultAudioDevice();  
  21. sre.RecognizeAsync(RecognizeMode.Multiple);  
  22. }  
  23. catch (Exception ex)  
  24. {  
  25. MessageBox.Show(ex.Message, "Error");  
  26. }  
  27. }  
  28. private void WellDone()  
  29. {  
  30. timer1.Start();  
  31. sre.RecognizeAsyncStop();  
  32. label1.Visible = false;  
  33. string path = @"C:\Users\Tan Wei Shen\Desktop\FYP\correct\";  
  34. string[] filePaths = Directory.GetFiles(path, "*.png");  
  35. pictureBox1.Image = Image.FromFile(filePaths[counter_CorrectAns]);   
  36. ss.Speak("Well done");  
  37. counter++;  
  38. }  
  39. public Form1()  
  40. {  
  41. InitializeComponent();  
  42. }   
  43. private void Form1_Load(object sender, EventArgs e)  
  44. {     
  45. }   
  46. private void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)  
  47. {  
  48. switch (e.Result.Text.ToString())  
  49. {  
  50. case "apple":     
  51. WellDone();     
  52. break;  
  53. case "chicken":     
  54. WellDone();     
  55. break;  
  56. case "eraser":     
  57. WellDone();     
  58. break;  
  59. }  
  60. }   
  61. private void btnStart_Click(object sender, EventArgs e)  
  62. {  
  63. btnStart.Visible = false;  
  64. label1.Visible = true;  
  65. pictureBox1.Visible = true;  
  66. Question();  
  67. }   
  68. private void timer1_Tick(object sender, EventArgs e)  
  69. {  
  70. Question();  
  71. timer1.Stop();  
  72. }  

Answers (1)