0

I'm trying to use SpeechRecognitionEngine(). When I say hello the program should respond with hi. The problem is when I run the program the there is an error:

Additional information: The language for the grammar does not match the language of the speech recognizer.

I don't know how to fix that on windows form application. Can someone help? Another problem is even if I make the program speech the voice gender is not female. Why is that?

Thanks in advance.

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

namespace test1
{
public partial class Form1 : Form
{
    SpeechSynthesizer s = new SpeechSynthesizer();
    Choices list = new Choices();

    public Form1()
    {
        SpeechRecognitionEngine rec = new SpeechRecognitionEngine();

        list.Add("hello");

        Grammar gr = new Grammar(new GrammarBuilder(list));

        rec.RequestRecognizerUpdate();
        rec.LoadGrammar(gr);
        rec.SpeechRecognized += rec_SpeechRecognized;
        rec.SetInputToDefaultAudioDevice();
        rec.RecognizeAsync(RecognizeMode.Multiple);

        InitializeComponent();
    }

    private void rec_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        s.SelectVoiceByHints(VoiceGender.Female);
        String r = e.Result.Text;

        if ( r == "hello" )
        {
            s.Speak("hi");
        }
    }
}
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Svetlozar
  • 967
  • 2
  • 10
  • 23

1 Answers1

0

Reading the MSDN article leads me to believe you have a mismatch in the expected culture of the GrammarBuilder object and the SpeechRecognitionEngine object.

To resolve you could simply specify the intended culture when initialising these objects. e.g.

    var enUS = new System.Globalization.CultureInfo("en-US");
    SpeechRecognitionEngine rec = new SpeechRecognitionEngine(enUS);
    GrammarBuilder gb = new GrammarBuilder
    {
         Culture = enUS;
    }
    gb.Append("hello");

Also from MSDN, ensure you have a speech recognition engine that supports your desired language-country code installed. This is generally the case with your standard Windows installation.

Microsoft Windows and the System.Speech API accept all valid language-country codes. To perform speech recognition using the language specified in the Culture property, a speech recognition engine that supports that language-country code must be installed.

aaronedmistone
  • 929
  • 10
  • 17