How do I stop Windows.Media.SpeechRecognition from deactivating on window focus change in a C# Console App?

Tristan Bailey 30 Reputation points
2025-01-23T07:47:09.7166667+00:00

I have been working on a speech recognition program for my grandfather to use, as he became blind a couple of years ago. This is a console application using C# and .NET Framework 4.8.1 that is designed to take various commands from him. Up until quite recently, it was using the System.Speech.Recognition namespace, which was working fine, until I helped my grandfather to purchase a brand new laptop with the latest build of Windows 11 to use the program on. The program failed to work and raised an error, which I found peculiar as the same program works fine on my laptop (also with Windows 11). I noticed that I could not see any related speech recognition settings in the 'Ease of Access' section of the Control Panel in Windows. I then discovered that the namespace has been made obsolete over a year ago with the latest builds of Windows 11.

Realising that I could no longer use the namespace, it left me searching for an alternative API that does not need an internet connection. After trying to use the Windows.Media.SpeechRecognition namespace in a test project, I found several things wrong with it. The biggest issue is that when the program's window changes focus to another window, the speech recognition engine deactivates (as can be seen by the microphone symbol disappearing in the lower right hand corner of the screen), ending the continuous recognition session that the program had started. Sometimes it deactivates immediately, and other times it takes several seconds to happen (in some cases, more than 10 seconds).

While the chance of the program losing focus is low, it could still happen unintentionally by an accidental mouse click or key stroke. A blind person is not able to tell whether or not the program has simply stopped listening for speech, so this has to be fixed.

When the speech recognition engine deactivates, there is no indication of this change in state by the speech recognition engine. Even though my program is registered to receive events such as the SpeechRecognizer_StateChanged and ContinuousRecognitionSession_Completed events, neither of these are raised when it deactivates. So, then I tried using the Automation API in Windows to receive an event when the window loses focus to get it to try to reactivate the engine. In the process of testing, I discovered that the engine will automatically deactivate regardless of the window being focused upon, even if it is reactivated manually while the focus is on another window, and then changes to yet another window not belonging to the program. Furthermore, if I try to read the state of the engine, the State property is not updated from 'SoundEnded'. Also, I thought I would try checking the IsEnabled property of the main constraint being used by the program, but this does not change from True.

I am now tearing my hair out trying to find a solution, as I have tried several ideas, and have spent the last 2 weeks trying. I came up with another idea about checking to see if the program is still using the microphone, but not sure how to go about this as I am not used to using audio or media APIs in Windows. Even if I managed to get the speech recognition engine to automatically reactivate at the correct time, it still doesn't stop it from deactivating automatically, which degrades the reliability of the program as it will still occasionally fail to pick up a command.

Places on the internet that reference the same issue:

https://stackoverflow.com/questions/50277331/why-does-uwp-continuous-speech-recognition-stop

https://discussions.unity.com/t/unity-speech-recognition-doesnt-work-while-program-is-running-in-background/858671

https://github.com/microsoft/WindowsAppSDK/issues/107

How can I prevent the speech recognition engine from deactivating when a window changes focus?

Windows for business | Windows Client for IT Pros | User experience | Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Tristan Bailey 30 Reputation points
    2025-02-27T10:15:16.2866667+00:00

    Here's another question for you guys:

    Does Microsoft even care about its user base anymore?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.