本文介绍了语音语音合成器& speechrecognizer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 Am enhancing en existing web application which makes the visually impaired people, to be used based on the Text to Speech & Speech Recognition



Here is the code, please do the needful.










using System.Speech;
using System.Speech.Synthesis;
using System.Threading;
using System.Speech.Recognition;



 SpeechSynthesizer ss = new SpeechSynthesizer();
    PromptBuilder pb = new PromptBuilder();
    SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
    SpeechRecognizer sr = new SpeechRecognizer();

   protected void btn_Spk_Click(object sender, EventArgs e)
    {
        string optiona, optionb, optionc;
        Choices clist = new Choices();
        foreach (GridViewRow row in gvtemperament.Rows)
        {
            RadioButton rblist1 = ((RadioButton)row.FindControl("rdTemp_Opt1"));
            RadioButton rblist2 = ((RadioButton)row.FindControl("rdTemp_Opt2"));
            RadioButton rblist3 = ((RadioButton)row.FindControl("rdTemp_Opt3"));
            Label lbl1 = (Label)row.FindControl("lbl_desc");
            if (row.Cells[0].Text == "1")
            {
                ss.SelectVoiceByHints(VoiceGender.Female);
                ss.Volume = 100;
                ss.Rate = -3;
                pb.ClearContent();
                pb.AppendText("Please answer all the Questions and you starts here!");
                ss.Speak(pb);
                Thread.Sleep(1500);

                pb.ClearContent();
                pb.AppendText(lbl1.Text);
                ss.Speak(pb);
                Thread.Sleep(1500);

                ss.SelectVoiceByHints(VoiceGender.Male);
                pb.ClearContent();
                pb.AppendText("Options are ");
                ss.Speak(pb);

                optiona = rblist1.Text;
                optionb = rblist2.Text;
                optionc = "Not Sure";


                pb.ClearContent();
                pb.AppendText(optiona);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionb);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionc);
                ss.Speak(pb);

                Thread.Sleep(150);

                clist.Add(new string[] { "Yes", "No", "Not Sure" });
                Grammar gr = new Grammar(new GrammarBuilder(clist));
                try
                {
                    sre.RequestRecognizerUpdate();
                    sre.LoadGrammar(gr);
                    sre.SpeechRecognized += sre_SpeechRecognized;
                    sre.SetInputToDefaultAudioDevice();
                    sre.RecognizeAsync(RecognizeMode.Multiple);
                }
                catch (Exception ex)
                {
                     this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language=\"javascript\">" + "alert('Error');</script>");
                }
            }
            else if (row.Cells[0].Text == "2")
            {
                ss.SelectVoiceByHints(VoiceGender.Female);
                ss.Volume = 100;
                ss.Rate = -3;

                pb.ClearContent();
                pb.AppendText(lbl1.Text);
                ss.Speak(pb);
                Thread.Sleep(1500);

                ss.SelectVoiceByHints(VoiceGender.Male);
                pb.ClearContent();
                pb.AppendText("Options are ");
                ss.Speak(pb);

                optiona = rblist1.Text;
                optionb = rblist2.Text;
                optionc = "Not Sure";


                pb.ClearContent();
                pb.AppendText(optiona);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionb);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionc);
                ss.Speak(pb);

                Thread.Sleep(150);
                clist.Add(new string[] { "Yes", "No", "Not Sure" });
                Grammar gr = new Grammar(new GrammarBuilder(clist));
                try
                {
                    sre.RequestRecognizerUpdate();
                    sre.LoadGrammar(gr);
                    sre.SpeechRecognized += sre_SpeechRecognized;
                    sre.SetInputToDefaultAudioDevice();
                    sre.RecognizeAsync(RecognizeMode.Multiple);
                }
                catch (Exception ex)
                {
                     this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language=\"javascript\">" + "alert('Error');</script>");
                }
            }
            else if (row.Cells[0].Text == "3")
            {
                ss.SelectVoiceByHints(VoiceGender.Female);
                ss.Volume = 100;
                ss.Rate = -3;

                pb.ClearContent();
                pb.AppendText(lbl1.Text);
                ss.Speak(pb);
                Thread.Sleep(1500);

                ss.SelectVoiceByHints(VoiceGender.Male);
                pb.ClearContent();
                pb.AppendText("Options are ");
                ss.Speak(pb);

                optiona = rblist1.Text;
                optionb = rblist2.Text;
                optionc = "Not Sure";

                pb.ClearContent();
                pb.AppendText(optiona);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionb);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionc);
                ss.Speak(pb);
                Thread.Sleep(150);
                clist.Add(new string[] { "Yes", "No", "Not Sure" });
                Grammar gr = new Grammar(new GrammarBuilder(clist));
                try
                {
                    sre.RequestRecognizerUpdate();
                    sre.LoadGrammar(gr);
                    sre.SpeechRecognized += sre_SpeechRecognized;
                    sre.SetInputToDefaultAudioDevice();
                    sre.RecognizeAsync(RecognizeMode.Multiple);
                }
                catch (Exception ex)
                {
                      this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language=\"javascript\">" + "alert('Error');</script>");
                }
            }
            else if (row.Cells[0].Text == "4")
            {
                ss.SelectVoiceByHints(VoiceGender.Female);
                ss.Volume = 100;
                ss.Rate = -3;

                pb.ClearContent();
                pb.AppendText(lbl1.Text);
                ss.Speak(pb);
                Thread.Sleep(1500);

                ss.SelectVoiceByHints(VoiceGender.Male);
                pb.ClearContent();
                pb.AppendText("Options are ");
                ss.Speak(pb);

                optiona = rblist1.Text;
                optionb = rblist2.Text;
                optionc = "Not Sure";

                pb.ClearContent();
                pb.AppendText(optiona);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionb);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionc);
                ss.Speak(pb);
                Thread.Sleep(150);
                clist.Add(new string[] { "Yes", "No", "Not Sure" });
                Grammar gr = new Grammar(new GrammarBuilder(clist));
                try
                {
                    sre.RequestRecognizerUpdate();
                    sre.LoadGrammar(gr);
                    sre.SpeechRecognized += sre_SpeechRecognized;
                    sre.SetInputToDefaultAudioDevice();
                    sre.RecognizeAsync(RecognizeMode.Multiple);
                }
                catch (Exception ex)
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language=\"javascript\">" + "alert('Error');</script>");
                }
            }
            else if (row.Cells[0].Text == "5")
            {
                ss.SelectVoiceByHints(VoiceGender.Female);
                ss.Volume = 100;
                ss.Rate = -3;
                pb.ClearContent();
                pb.AppendText(lbl1.Text);
                ss.Speak(pb);
                Thread.Sleep(1500);

                ss.SelectVoiceByHints(VoiceGender.Male);
                pb.ClearContent();
                pb.AppendText("Options are ");
                ss.Speak(pb);

                optiona = rblist1.Text;
                optionb = rblist2.Text;
                optionc = "Not Sure";

                pb.ClearContent();
                pb.AppendText(optiona);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionb);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionc);
                ss.Speak(pb);
                Thread.Sleep(150);
                clist.Add(new string[] { "Yes", "No", "Not Sure" });
                Grammar gr = new Grammar(new GrammarBuilder(clist));
                try
                {
                    sre.RequestRecognizerUpdate();
                    sre.LoadGrammar(gr);
                    sre.SpeechRecognized += sre_SpeechRecognized;
                    sre.SetInputToDefaultAudioDevice();
                    sre.RecognizeAsync(RecognizeMode.Multiple);
                }
                catch (Exception ex)
                {
                     this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language=\"javascript\">" + "alert('Error');</script>");
                }
            }
            else if (row.Cells[0].Text == "6")
            {
                ss.SelectVoiceByHints(VoiceGender.Female);
                ss.Volume = 100;
                ss.Rate = -3;
                pb.ClearContent();
                pb.AppendText(lbl1.Text);
                ss.Speak(pb);
                Thread.Sleep(1500);

                ss.SelectVoiceByHints(VoiceGender.Male);
                pb.ClearContent();
                pb.AppendText("Options are ");
                ss.Speak(pb);

                optiona = rblist1.Text;
                optionb = rblist2.Text;
                optionc = "Not Sure";

                pb.ClearContent();
                pb.AppendText(optiona);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionb);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionc);
                ss.Speak(pb);
                Thread.Sleep(150);
                clist.Add(new string[] { "Yes", "No", "Not Sure" });
                Grammar gr = new Grammar(new GrammarBuilder(clist));
                try
                {
                    sre.RequestRecognizerUpdate();
                    sre.LoadGrammar(gr);
                    sre.SpeechRecognized += sre_SpeechRecognized;
                    sre.SetInputToDefaultAudioDevice();
                    sre.RecognizeAsync(RecognizeMode.Multiple);
                }
                catch (Exception ex)
                {
                     this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language=\"javascript\">" + "alert('Error');</script>");
                }
            }
            else if (row.Cells[0].Text == "7")
            {
                ss.SelectVoiceByHints(VoiceGender.Female);
                ss.Volume = 100;
                ss.Rate = -3;
                pb.ClearContent();
                pb.AppendText(lbl1.Text);
                ss.Speak(pb);
                Thread.Sleep(1500);

                ss.SelectVoiceByHints(VoiceGender.Male);
                pb.ClearContent();
                pb.AppendText("Options are ");
                ss.Speak(pb);

                optiona = rblist1.Text;
                optionb = rblist2.Text;
                optionc = "Not Sure";

                pb.ClearContent();
                pb.AppendText(optiona);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionb);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionc);
                ss.Speak(pb);
                Thread.Sleep(150);
                clist.Add(new string[] { "Yes", "No", "Not Sure" });
                Grammar gr = new Grammar(new GrammarBuilder(clist));
                try
                {
                    sre.RequestRecognizerUpdate();
                    sre.LoadGrammar(gr);
                    sre.SpeechRecognized += sre_SpeechRecognized;
                    sre.SetInputToDefaultAudioDevice();
                    sre.RecognizeAsync(RecognizeMode.Multiple);
                }
                catch (Exception ex)
                {
                     this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language=\"javascript\">" + "alert('Error');</script>");
                }
            }
            else if (row.Cells[0].Text == "8")
            {
                ss.SelectVoiceByHints(VoiceGender.Female);
                ss.Volume = 100;
                ss.Rate = -3;
                pb.ClearContent();
                pb.AppendText(lbl1.Text);
                ss.Speak(pb);
                Thread.Sleep(1500);

                ss.SelectVoiceByHints(VoiceGender.Male);
                pb.ClearContent();
                pb.AppendText("Options are ");
                ss.Speak(pb);

                optiona = rblist1.Text;
                optionb = rblist2.Text;
                optionc = "Not Sure";

                pb.ClearContent();
                pb.AppendText(optiona);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionb);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionc);
                ss.Speak(pb);
                Thread.Sleep(150);
                clist.Add(new string[] { "Yes", "No", "Not Sure" });
                Grammar gr = new Grammar(new GrammarBuilder(clist));
                try
                {
                    sre.RequestRecognizerUpdate();
                    sre.LoadGrammar(gr);
                    sre.SpeechRecognized += sre_SpeechRecognized;
                    sre.SetInputToDefaultAudioDevice();
                    sre.RecognizeAsync(RecognizeMode.Multiple);
                }
                catch (Exception ex)
                {
                      this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language=\"javascript\">" + "alert('Error');</script>");
                }
            }
            else if (row.Cells[0].Text == "9")
            {
                ss.SelectVoiceByHints(VoiceGender.Female);
                ss.Volume = 100;
                ss.Rate = -3;
                pb.ClearContent();
                pb.AppendText(lbl1.Text);
                ss.Speak(pb);
                Thread.Sleep(1500);

                ss.SelectVoiceByHints(VoiceGender.Male);
                pb.ClearContent();
                pb.AppendText("Options are ");
                ss.Speak(pb);

                optiona = rblist1.Text;
                optionb = rblist2.Text;
                optionc = "Not Sure";

                pb.ClearContent();
                pb.AppendText(optiona);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionb);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionc);
                ss.Speak(pb);
                Thread.Sleep(150);
                clist.Add(new string[] { "Yes", "No", "Not Sure" });
                Grammar gr = new Grammar(new GrammarBuilder(clist));
                try
                {
                    sre.RequestRecognizerUpdate();
                    sre.LoadGrammar(gr);
                    sre.SpeechRecognized += sre_SpeechRecognized;
                    sre.SetInputToDefaultAudioDevice();
                    sre.RecognizeAsync(RecognizeMode.Multiple);
                }
                catch (Exception ex)
                {
                     this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language=\"javascript\">" + "alert('Error');</script>");
                }
            }
            else if (row.Cells[0].Text == "10")
            {
                ss.SelectVoiceByHints(VoiceGender.Female);
                ss.Volume = 100;
                ss.Rate = -3;
                pb.ClearContent();
                pb.AppendText(lbl1.Text);
                ss.Speak(pb);
                Thread.Sleep(1500);

                ss.SelectVoiceByHints(VoiceGender.Male);
                pb.ClearContent();
                pb.AppendText("Options are ");
                ss.Speak(pb);

                optiona = rblist1.Text;
                optionb = rblist2.Text;
                optionc = "Not Sure";

                pb.ClearContent();
                pb.AppendText(optiona);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionb);
                ss.Speak(pb);
                Thread.Sleep(700);

                pb.ClearContent();
                pb.AppendText(optionc);
                ss.Speak(pb);
                Thread.Sleep(150);
                clist.Add(new string[] { "Yes", "No", "Not Sure" });
                Grammar gr = new Grammar(new GrammarBuilder(clist));
                try
                {
                    sre.RequestRecognizerUpdate();
                    sre.LoadGrammar(gr);
                    sre.SpeechRecognized += sre_SpeechRecognized;
                    sre.SetInputToDefaultAudioDevice();
                    sre.RecognizeAsync(RecognizeMode.Multiple);
                }
                catch (Exception ex)
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script language=\"javascript\">" + "alert('Error');</script>");
                }
            }
        }
    }

  void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
  {
        foreach (GridViewRow row in gvtemperament.Rows)
        {
            RadioButton rblist1 = ((RadioButton)row.FindControl("rdTemp_Opt1"));
            RadioButton rblist2 = ((RadioButton)row.FindControl("rdTemp_Opt2"));
            RadioButton rblist3 = ((RadioButton)row.FindControl("rdTemp_Opt3"));
            Label lbl1 = (Label)row.FindControl("lbl_desc");

            if (row.Cells[0].Text == "1")
            {
                if (e.Result.Text == "Yes")
                {
                    rblist1.Checked = true;
                    rblist1.Text = "Narendran";
                }
                else if (e.Result.Text == "No")
                {
                    rblist2.Checked = true;
                        rblist1.Text = "Naren";
                }
                else //if (e.Result.Text == "Not Sure")
                {
                    rblist3.Checked = true;
                    rblist1.Text = "Chk";
                }
            }
            else if (row.Cells[0].Text == "2")
            {
                if (e.Result.Text == "Yes")
                {
                    rblist1.Checked = true;
                    rblist1.Text = "Round2";
                }
                else if (e.Result.Text == "No")
                {
                    rblist2.Checked = true;
                    rblist2.Text = "Round2";
                }
                else //if (e.Result.Text == "Not Sure")
                {
                    rblist3.Checked = true;
                    rblist3.Text = "Round2";
                }
            }

            else if (row.Cells[0].Text == "3")
            {
                if (e.Result.Text == "Yes")
                {
                    rblist1.Checked = true;
                }
                else if (e.Result.Text == "No")
                {
                    rblist2.Checked = true;
                }
                else //if (e.Result.Text == "Not Sure")
                {
                    rblist3.Checked = true;
                }
            }

            else if (row.Cells[0].Text == "4")
            {
                if (e.Result.Text == "Yes")
                {
                    rblist1.Checked = true;
                }
                else if (e.Result.Text == "No")
                {
                    rblist2.Checked = true;
                }
                else //if (e.Result.Text == "Not Sure")
                {
                    rblist3.Checked = true;
                }
            }

            else if (row.Cells[0].Text == "5")
            {
                if (e.Result.Text == "Yes")
                {
                    rblist1.Checked = true;
                }
                else if (e.Result.Text == "No")
                {
                    rblist2.Checked = true;
                }
                else //if (e.Result.Text == "Not Sure")
                {
                    rblist3.Checked = true;
                }
            }

            else if (row.Cells[0].Text == "6")
            {
                if (e.Result.Text == "Yes")
                {
                    rblist1.Checked = true;
                }
                else if (e.Result.Text == "No")
                {
                    rblist2.Checked = true;
                }
                else //if (e.Result.Text == "Not Sure")
                {
                    rblist3.Checked = true;
                }
            }
            else if (row.Cells[0].Text == "7")
            {
                if (e.Result.Text == "Yes")
                {
                    rblist1.Checked = true;
                }
                else if (e.Result.Text == "No")
                {
                    rblist2.Checked = true;
                }
                else //if (e.Result.Text == "Not Sure")
                {
                    rblist3.Checked = true;
                }
            }

            else if (row.Cells[0].Text == "8")
            {
                if (e.Result.Text == "Yes")
                {
                    rblist1.Checked = true;
                }
                else if (e.Result.Text == "No")
                {
                    rblist2.Checked = true;
                }
                else //if (e.Result.Text == "Not Sure")
                {
                    rblist3.Checked = true;
                }
            }

            else if (row.Cells[0].Text == "9")
            {
                if (e.Result.Text == "Yes")
                {
                    rblist1.Checked = true;
                }
                else if (e.Result.Text == "No")
                {
                    rblist2.Checked = true;
                }
                else //if (e.Result.Text == "Not Sure")
                {
                    rblist3.Checked = true;
                }
            }

            else if (row.Cells[0].Text == "10")
            {
                if (e.Result.Text == "Yes")
                {
                    rblist1.Checked = true;
                }
                else if (e.Result.Text == "No")
                {
                    rblist2.Checked = true;
                }
                else //if (e.Result.Text == "Not Sure")
                {
                    rblist3.Checked = true;
                }
            }
            else
            {

            }
        }
    }  





Please do the needful.

Narendran Namachivayam.



What I have tried:



The same code is working fine in windows application, but not in web application.

If I put break point, it’s coming to the point but the code is not getting execute.



Please do the needful.
Narendran Namachivayam.

What I have tried:

The same code is working fine in windows application, but not in web application.
If I put break point, it's coming to the point but the code is not getting execute.

推荐答案




这篇关于语音语音合成器&amp; speechrecognizer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 15:40