none
UWP C#: speech recognition giving an exception when i enter voice input RRS feed

  • Question

  • public sealed partial class MainPage : Page { private SpeechRecognizer MyRecognizer; SpeechSynthesizer speechSynthesizer; public MainPage() { this.InitializeComponent(); InitializeSpeechRecognizer(); speechSynthesizer = new SpeechSynthesizer(); } private async void InitializeSpeechRecognizer() { // Initialize SpeechRecognizer Object MyRecognizer = new SpeechRecognizer(); // Register Event Handlers MyRecognizer.StateChanged += MyRecognizer_StateChanged; MyRecognizer.ContinuousRecognitionSession.ResultGenerated += MyRecognizer_ResultGenerated; // Create Grammar File Object StorageFile GrammarContentFile = await Package.Current.InstalledLocation.GetFileAsync(@"Grammar\MyGrammar.xml"); // Add Grammar Constraint from Grammar File SpeechRecognitionGrammarFileConstraint GrammarConstraint = new SpeechRecognitionGrammarFileConstraint(GrammarContentFile); MyRecognizer.Constraints.Add(GrammarConstraint); // Compile Grammar SpeechRecognitionCompilationResult CompilationResult = await MyRecognizer.CompileConstraintsAsync(); // Write Debug Information Debug.WriteLine("Status: " + CompilationResult.Status.ToString()); // If Compilation Successful, Start Continuous Recognition Session if (CompilationResult.Status == SpeechRecognitionResultStatus.Success) { await MyRecognizer.ContinuousRecognitionSession.StartAsync(); } } /* private async void Talk(string message) { try { var stream = await speechSynthesizer.SynthesizeTextToStreamAsync(message); media.SetSource(stream, stream.ContentType); media.Play(); } catch (Exception e) { Talk("error"); } } */ public void MyRecognizer_ResultGenerated(SpeechContinuousRecognitionSession sender, SpeechContinuousRecognitionResultGeneratedEventArgs args) { // Write Debug Information Debug.WriteLine(args.Result.Text); if (args.Result.Text =="move forward") { textBox.Text = "al"; } else { textBox.Text = "error"; } /* Drive robot on recognized speech switch (args.Result.Text) { case "move forward": Talk("working perfect"); break; case "move reverse": break; case "turn right": break; case "turn left": break; case "stop": break; case "engage obstacle detection": break; case "disengage obstacle detection": break; default: break; } */ } /// <param name="sender"></param> /// <param name="args"></param> private void MyRecognizer_StateChanged(SpeechRecognizer sender, SpeechRecognizerStateChangedEventArgs args) { // Write Debug Information Debug.WriteLine(args.State); } } }

    System.Exception: 'The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))'


    I dont know why i am getting is exception please help me, :)
    Sunday, May 28, 2017 12:37 PM