Skip to content

Conversation

@VladislavAntonyuk
Copy link
Collaborator

Speech Recognition Windows Ensure unsubscribe from events on Stop Recording

Copilot AI review requested due to automatic review settings June 12, 2025 07:08
@VladislavAntonyuk VladislavAntonyuk self-assigned this Jun 12, 2025
@VladislavAntonyuk VladislavAntonyuk requested review from a team and TheCodeTraveler June 12, 2025 07:08
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/InternalStopListening finally 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 StopRecording to prevent memory leaks.
speechRecognizer?.Dispose();

src/CommunityToolkit.Maui.Core/Essentials/SpeechToText/OfflineSpeechToTextImplementation.windows.cs:113

  • Switching from synchronous LoadGrammar to LoadGrammarAsync without awaiting the load can lead to recognition starting before the grammar is ready. Consider awaiting completion (e.g., handle the GrammarLoadCompleted event) before calling RecognizeAsync.
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();

Copy link
Collaborator

@TheCodeTraveler TheCodeTraveler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 TheCodeTraveler added pending documentation This feature requires documentation approved This Proposal has been approved and is ready to be added to the Toolkit labels Jun 12, 2025
@VladislavAntonyuk
Copy link
Collaborator Author

@TheCodeTraveler, it is already in the docs:
image

@TheCodeTraveler
Copy link
Collaborator

Nice! Could you also add a section that recommends using IDisposable or IAsyncDisposable?

@VladislavAntonyuk
Copy link
Collaborator Author

I don't think it's recommend. For example users may register their service as Singleton.

@TheCodeTraveler TheCodeTraveler merged commit a438997 into main Jun 14, 2025
10 checks passed
@TheCodeTraveler TheCodeTraveler deleted the speech-recognition-ensure-stopped branch June 14, 2025 05:06
@github-actions github-actions bot locked and limited conversation to collaborators Jun 15, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

approved This Proposal has been approved and is ready to be added to the Toolkit pending documentation This feature requires documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants