Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Learn how to handle audio input quality problems during speech recognition in your Windows App SDK app.
Important
Speech recognition requires MSIX package identity. The Windows.Media.SpeechRecognition APIs are available only when your app runs with package identity (packaged or packaged with external location). Unpackaged apps cannot use these APIs.
Key APIs
Overview
Audio input quality can affect the accuracy of speech recognition. Background noise, muffled microphones, and low volume are common causes of recognition failure. Handle the RecognitionQualityDegrading event to let the user know when audio quality may prevent accurate recognition.
Handle quality degradation
The RecognitionQualityDegrading event fires when audio issues may interfere with speech recognition. Use the SpeechRecognitionQualityDegradingEventArgs.Problem property to determine the specific issue.
Because this event fires on a background thread, use DispatcherQueue.TryEnqueue to update the UI:
private void SpeechRecognizer_RecognitionQualityDegrading(
SpeechRecognizer sender,
SpeechRecognitionQualityDegradingEventArgs args)
{
dispatcherQueue.TryEnqueue(() =>
{
// Show a message or indicator to the user.
// args.Problem identifies the specific audio quality issue.
switch (args.Problem)
{
case SpeechRecognitionAudioProblem.TooFast:
statusTextBlock.Text = "Try speaking more slowly.";
break;
case SpeechRecognitionAudioProblem.TooSlow:
statusTextBlock.Text = "Try speaking a bit faster.";
break;
case SpeechRecognitionAudioProblem.TooQuiet:
statusTextBlock.Text = "Try speaking louder or move closer to the microphone.";
break;
case SpeechRecognitionAudioProblem.TooLoud:
statusTextBlock.Text = "Try speaking more quietly or move farther from the microphone.";
break;
case SpeechRecognitionAudioProblem.TooNoisy:
statusTextBlock.Text = "Background noise is interfering with recognition. Try a quieter environment.";
break;
case SpeechRecognitionAudioProblem.None:
default:
statusTextBlock.Text = "";
break;
}
});
}
To use this handler, subscribe to the event after creating your recognizer:
speechRecognizer.RecognitionQualityDegrading += SpeechRecognizer_RecognitionQualityDegrading;
UI thread considerations
In WinUI 3, use DispatcherQueue instead of CoreDispatcher to marshal calls to the UI thread. Get a reference to the DispatcherQueue for the current thread in your page constructor or initialization logic:
private Microsoft.UI.Dispatching.DispatcherQueue dispatcherQueue =
Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread();
Then use dispatcherQueue.TryEnqueue(...) in your event handlers to safely update UI elements.
Related articles
Windows developer