Quickstart: Recognize speech with the Speech SDK for .NET Framework (Windows)
Use this guide to create a speech-to-text console application using the .NET framework for Windows and the Speech SDK. When finished, you can use your computer's microphone to transcribe speech to text in real time.
Prerequisites
To complete this project, you'll need:
- Visual Studio 2017
- A subscription key for the Speech Service. Get one for free.
- Access to your computer's microphone
Create a Visual Studio project
Start Visual Studio 2017.
From the menu bar in Visual Studio, select Tools > Get Tools and make sure that the .Net desktop development workload is available. If the workload hasn't been installed, mark the checkbox, then click Modify to start the installation. It may take a few minutes to download and install.
If the checkbox next to .NET desktop development is selected, you can close the dialog box now.
Next, let's create a project. From the menu bar select File > New > Project. When the dialog box appears, from the left panel expand these sections Installed > Visual C# > Windows Desktop and select Console App (.NET Framework). Name this project helloworld.
Now that the project is set up, we need to install the Speech SDK NuGet package and reference it in our code. Locate the Solution Explorer and right-click on helloworld. From the menu, select Manage NuGet Packages....
In the upper-right corner of the NuGet Package Manager, locate the Package Source dropdown and make sure that nuget.org is selected. Then, select Browse and search for the
Microsoft.CognitiveServices.Speech
package and install the latest stable version.Accept all agreements and licenses to start the installation.
After the package is installed, a confirmation appears in the Package Manager console.
The next step is to create a platform configuration that matches the architecture of the computer you're using to build and run the console application. From the menu bar, select Build > Configuration Manager....
In the Configuration Manager dialog box, locate the Active solution platform drop-down list, and select New.
If you are running 64-bit Windows, when prompted with Type or select the new platform,
x64
. If you are running 32-bit Windows, selectx86
. When you're finished, click OK.
Add sample code
Open
Program.cs
and replace the automatically generated code with this sample:using System; using System.Threading.Tasks; using Microsoft.CognitiveServices.Speech; namespace helloworld { class Program { public static async Task RecognizeSpeechAsync() { // Creates an instance of a speech config with specified subscription key and service region. // Replace with your own subscription key and service region (e.g., "westus"). var config = SpeechConfig.FromSubscription("YourSubscriptionKey", "YourServiceRegion"); // Creates a speech recognizer. using (var recognizer = new SpeechRecognizer(config)) { Console.WriteLine("Say something..."); // Performs recognition. RecognizeOnceAsync() returns when the first utterance has been recognized, // so it is suitable only for single shot recognition like command or query. For long-running // recognition, use StartContinuousRecognitionAsync() instead. var result = await recognizer.RecognizeOnceAsync(); // Checks result. if (result.Reason == ResultReason.RecognizedSpeech) { Console.WriteLine($"We recognized: {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?"); } } } } static void Main() { RecognizeSpeechAsync().Wait(); Console.WriteLine("Please press a key to continue."); Console.ReadLine(); } } }
Locate and replace the string
YourSubscriptionKey
with your Speech Service subscription key.Locate and replace the string
YourServiceRegion
with the region associated with your subscription. For example, if you're using the free trial, the region iswestus
.Save the changes to the project.
Build and run the app
From the menu bar, select Build > Build Solution. The code should compile without errors now.
From the menu bar, select Debug > Start Debugging, or press F5 to start the application.
A console window will appear, prompting you to speak. Now, say something in English. Your speech is transmitted to the Speech Service and transcribed to text in real time. The result is printed to the console.
Next steps
See also
Feedback
We'd love to hear your thoughts. Choose the type you'd like to provide:
Our feedback system is built on GitHub Issues. Read more on our blog.
Loading feedback...