Speech Recognition Windows Ensure unsubscribe from events on Stop Recording#2705
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR ensures that speech recognizers on Windows clean up event subscriptions and are disposed when stopping recording. It also updates offline recognizer startup logic, adds disposal to the sample view model, and declares the microphone capability in the sample app manifest.
- Unsubscribe from events and dispose recognizers in
StopRecording/InternalStopListeningfinally blocks. - Only start recognition when the recognizer is idle (online) or stopped (offline).
- Make the sample view model disposable and add microphone capability to the manifest.
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/CommunityToolkit.Maui.Core/Essentials/SpeechToText/SpeechToTextImplementation.windows.cs | Move event unsubscriptions and disposal into StopRecording finally block; guard start on idle state. |
| src/CommunityToolkit.Maui.Core/Essentials/SpeechToText/OfflineSpeechToTextImplementation.windows.cs | Mirror cleanup in InternalStopListening, switch to LoadGrammarAsync, and guard RecognizeAsync on stopped state. |
| samples/CommunityToolkit.Maui.Sample/ViewModels/Essentials/SpeechToTextViewModel.cs | Implement IAsyncDisposable for speech-to-text cleanup; simplify default locale selection. |
| samples/CommunityToolkit.Maui.Sample/Platforms/Windows/Package.appxmanifest | Add <DeviceCapability Name="microphone"/> under <Capabilities>. |
Comments suppressed due to low confidence (3)
src/CommunityToolkit.Maui.Core/Essentials/SpeechToText/SpeechToTextImplementation.windows.cs:108
- Consider adding unit tests to verify that event handlers are properly removed and that the recognizer is disposed after
StopRecordingto prevent memory leaks.
speechRecognizer?.Dispose();
src/CommunityToolkit.Maui.Core/Essentials/SpeechToText/OfflineSpeechToTextImplementation.windows.cs:113
- Switching from synchronous
LoadGrammartoLoadGrammarAsyncwithout awaiting the load can lead to recognition starting before the grammar is ready. Consider awaiting completion (e.g., handle theGrammarLoadCompletedevent) before callingRecognizeAsync.
offlineSpeechRecognizer.LoadGrammarAsync(new DictationGrammar());
samples/CommunityToolkit.Maui.Sample/ViewModels/Essentials/SpeechToTextViewModel.cs:56
- Removing the platform-specific default-language fallback changes the initial locale selection behavior. If intentional, add a comment; otherwise restore the previous logic or provide a configurable fallback.
CurrentLocale = Locales.FirstOrDefault();
TheCodeTraveler
left a comment
There was a problem hiding this comment.
Thanks Vlad! I see you've added a few items to the sample app, like <DeviceCapability Name="microphone"/> and IAsyncDisposable.
Could you also open a Docs PR to include these before we merge this PR?
|
@TheCodeTraveler, it is already in the docs: |
|
Nice! Could you also add a section that recommends using |
|
I don't think it's recommend. For example users may register their service as Singleton. |

Speech Recognition Windows Ensure unsubscribe from events on Stop Recording