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();
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.