using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Drawing; using System.Globalization; using System.Linq; using System.Speech.Recognition; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace ManagedSapiTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { this.textBox1.Text += "CurrentCulture " + CultureInfo.CurrentCulture + Environment.NewLine; this.textBox1.Text += "CurrentUICulture " + CultureInfo.CurrentUICulture + Environment.NewLine; foreach (RecognizerInfo ri in SpeechRecognitionEngine.InstalledRecognizers()) { this.textBox1.Text += String.Format("Id={0}, Name={1}, Description={2}, Culture={3}", ri.Id, ri.Name, ri.Description, ri.Culture) + Environment.NewLine; foreach(string key in ri.AdditionalInfo.Keys) { this.textBox1.Text += string.Format("{0} = {1}", key, ri.AdditionalInfo[key]) + Environment.NewLine; } } this.rec = new SpeechRecognitionEngine(GetRecognizer()); //this line crashes this.rec.SpeechRecognized += rec_SpeechRecognized; this.rec.SpeechDetected += rec_SpeechDetected; var gb = new GrammarBuilder(); gb.Culture = CultureInfo.CurrentCulture; var c = new Choices("test"); gb.Append(c); this.rec.LoadGrammar(new Grammar(gb)); this.rec.SetInputToDefaultAudioDevice(); this.rec.RecognizeAsync(RecognizeMode.Multiple); } private static RecognizerInfo GetRecognizer() { foreach (RecognizerInfo recognizer in SpeechRecognitionEngine.InstalledRecognizers()) { if (Thread.CurrentThread.CurrentCulture.Name.Equals(recognizer.Culture.Name, StringComparison.OrdinalIgnoreCase)) { return recognizer; } } MessageBox.Show("Couldn't locate a suitable recognizer!"); return null; } void rec_SpeechDetected(object sender, SpeechDetectedEventArgs e) { Debug.WriteLine("rec_SpeechDetected"); } void rec_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) { Debug.WriteLine("rec_SpeechRecognized"); this.Invoke((MethodInvoker)delegate { this.Text = e.Result.Text; }); } System.Speech.Recognition.SpeechRecognitionEngine rec; } }