在C#中没有预定义选择的语音识别

时间:2015-04-29 09:52:19

标签: c# winforms speech-recognition speech-to-text

我正在尝试使用C#开发SpeechRecognition演示应用。目前我的代码是 -

public partial class Form1 : Form
{
    SpeechSynthesizer ss = new SpeechSynthesizer();
    PromptBuilder pb = new PromptBuilder();
    SpeechRecognitionEngine sre = new SpeechRecognitionEngine();
    Choices choices;
    public Form1()
    {
        InitializeComponent();
    }

    private void btnStart_Click(object sender, EventArgs e)
    {
        choices = new Choices();
        choices.Add(new string[]{"hello","how are you","thank you"});
        Grammar gr = new Grammar(new GrammarBuilder(choices));
        try
        {
            sre.RequestRecognizerUpdate();
            sre.LoadGrammar(gr);
            sre.SpeechRecognized += sre_SpeechRecognized;
            sre.SetInputToDefaultAudioDevice();
            sre.RecognizeAsync(RecognizeMode.Multiple);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message,"Error");
        }
    }

    void sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        switch (e.Result.Text.ToString())
        {
            case "hello":
                ss.SpeakAsync("hello");
                break;
            case "how are you":
                ss.SpeakAsync("how are you");
                break;
            case "thank you":
                ss.SpeakAsync("thank you");
                break;
            default:
                break;
        }

        txtVoiceToText.Text += e.Result.Text.ToString() + Environment.NewLine;
    }
}

如果我不想使用任何预定义的Choices,有什么办法吗?

1 个答案:

答案 0 :(得分:1)

在DictationMode中尝试SpeechRecognition。您可以看到示例here