0

I am making speech to text app in C# window form it was working fine and running in vs but I

want to make I have add a button in my application I want to make when I click on the button it start recognizing when I clicked on the button again the recognizing will stop in C#

How can I solve this?

code here

using Microsoft.CognitiveServices.Speech;
using Microsoft.CognitiveServices.Speech.Audio;
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;

namespace WindowsFormsApp5
{
    public partial class Form1 : Form
    {


        public static async Task RecognitionWithMicrophoneAsync()
        {
            
            var config = SpeechConfig.FromSubscription("key", "region");
            

            using (var recognizer = new SpeechRecognizer(config))
            {

                //Console.WriteLine("Say something...");

                var result = await recognizer.RecognizeOnceAsync().ConfigureAwait(false);

                // Checks result.
                if (result.Reason == ResultReason.RecognizedSpeech)

                {
                    //Console.WriteLine($"RECOGNIZED: Text={result.Text}");
                    SendKeys.SendWait(result.Text);
                }
                //else if (result.Reason == ResultReason.NoMatch)
                //{
                   // Console.WriteLine($"NOMATCH: Speech could not be recognized.");
                //}
                else if (result.Reason == ResultReason.Canceled)
                {
                    var cancellation = CancellationDetails.FromResult(result);
                    Console.WriteLine($"CANCELED: Reason={cancellation.Reason}");

                    if (cancellation.Reason == CancellationReason.Error)
                    {
                        Console.WriteLine($"CANCELED: ErrorCode={cancellation.ErrorCode}");
                        Console.WriteLine($"CANCELED: ErrorDetails={cancellation.ErrorDetails}");
                        Console.WriteLine($"CANCELED: Did you update the subscription info?");
                    }
                }
            }
            
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            RecognitionWithMicrophoneAsync().Wait();
            //Console.WriteLine("Please press enter to exit.");
            //Console.ReadLine();
        }
    }
}
Babar Ali
  • 133
  • 10
  • Add a Field `bool isRecognizing`, set to `true` when you start the recognizer, set to `false` before you call [SpeechRecognizer.StopContinuousRecognitionAsync()](https://learn.microsoft.com/en-us/dotnet/api/microsoft.cognitiveservices.speech.speechrecognizer.stopcontinuousrecognitionasync) -- This code is made for a Console app. Make you `button1_Click` handler `async` and `await RecognitionWithMicrophoneAsync();` (never use `Wait()` in WinFoms). Remove `ConfigureAwait(false)` and pass the recognized text to a Control. – Jimi Aug 05 '22 at 03:33
  • It looks like you have copied the example from [here](https://learn.microsoft.com/en-us/dotnet/api/microsoft.cognitiveservices.speech.speechrecognizer.recognizeonceasync). What's `SendKeys.SendWait(result.Text);` for? – Jimi Aug 05 '22 at 03:36
  • @Jimi can you type the code share in the comment please – Babar Ali Aug 05 '22 at 05:23
  • @Jimi SendKeys.SendWait(result.Text); when you say something it will recognize and type in a active window just like (notepad, word, PowerPoint, etc. )any where you can type – Babar Ali Aug 05 '22 at 06:00
  • 1
    `if (isRecognizing) { isRecognizing = false; await recognizer.StopContinuousRecognitionAsync(); } else { isRecognizing = true; await RecognitionWithMicrophoneAsync(); }` <- Implies that you also store the `SpeechRecognizer` object (`recognizer`) as a Field, otherwise you cannot use its methods -- Add to app.config `` (make sure it uses `SendInput()` to send chars to other applications, so `Send()` and `SendWait()` act the same way - no wait) – Jimi Aug 05 '22 at 12:37

0 Answers0