Skip to content

fix(SpeechRecognizer): keep Android recognizer reusable after stop - #26693

Open
morning4coffe-dev wants to merge 1 commit into
masterfrom
dev/doti/speechrecognizer-android-stop
Open

morning4coffe-dev wants to merge 1 commit into
masterfrom
dev/doti/speechrecognizer-android-stop

Conversation

@morning4coffe-dev

Copy link
Copy Markdown
Member

GitHub Issue: closes #26692

PR Type:

🐞 Bugfix

What changed? 🚀

On Android, SpeechRecognizer.StopRecognitionAsync() called Destroy() on the native Android.Speech.SpeechRecognizer, so the instance could not recognize again. Dispose() never destroyed it.

  • StopRecognitionAsync() now only calls StopListening(). That ends audio capture and still delivers the final results through the listener.
  • Dispose() calls Destroy() before disposing the native recognizer, and clears the field.

Validation: compile only (Uno.WinRT.netcoremobile for net10.0-android). Not run on a device or emulator.

PR Checklist ✅

StopRecognitionAsync destroyed the native recognizer, so the instance
could not recognize again. Stop listening only, and destroy on Dispose.
@github-actions github-actions Bot added the platform/android 🤖 Categorizes an issue or PR as relevant to the Android platform label Nov 24, 2026
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @morning4coffe-dev's task in 26s —— View job


Review summary

Verdict: ship (with two minor, non-blocking suggestions).

  • 🟢 Root-cause fix: the broken invariant was lifecycle ownership. Destroy() was called in StopRecognitionAsync(), which made the native recognizer unusable. Moving it to Dispose() is correct.
  • Issues: 0 blocking, 2 minor (both inline: one comment-style nit, one suggested null/disposed guard, which would be defensive hardening).

Validation evidence

  • Code review assessment: the logic is correct by inspection. StopListening() still delivers final results through the listener, so RecognizeAsync completes.
  • Compile validation: the PR author reports Uno.WinRT.netcoremobile for net10.0-android built. I did not rebuild it.
  • Runtime validation: none. The change has not been run on a device or emulator, and there is no automated test for it. A maintainer should run the SpeechRecognizerTests sample on Android to check stop and then recognize again.

Cross-cutting notes

  • The Android SpeechRecognizer must be used from the main thread. Dispose() now calls Destroy(), so callers must dispose on the UI thread. This is an existing constraint that was previously hit via Stop.

@mergify

mergify Bot commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.

  • Queue this pull request once it meets the queue conditions

Comment on lines +85 to +86
// StopListening ends audio capture and still delivers the final results. Destroy is
// reserved for Dispose: calling it here left the instance unable to recognize again.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit (code-style): the second line narrates change history ("calling it here left the instance unable to recognize again"). Per .claude/rules/code-style.md, comments should state the present reason only; git carries the history. Suggest a single line:

Suggested change
// StopListening ends audio capture and still delivers the final results. Destroy is
// reserved for Dispose: calling it here left the instance unable to recognize again.
// StopListening ends capture but still delivers final results; Destroy is reserved for Dispose.

{
_speechRecognizer?.Destroy();
_speechRecognizer?.Dispose();
_speechRecognizer = null;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since Dispose() now nulls _speechRecognizer, RecognizeTaskAsync (lines 57-58) dereferences it without a null check. Calling RecognizeAsync() after Dispose() will now throw a NullReferenceException instead of an ObjectDisposedException-style failure. Consider an ObjectDisposedException.ThrowIf(_speechRecognizer is null, this) at the start of RecognizeTaskAsync. This is defensive hardening only; the root-cause lifecycle fix (Destroy only in Dispose) is correct.

@unodevops

Copy link
Copy Markdown
Contributor

🤖 Your WebAssembly Sample App stage site is ready! Visit it here:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

platform/android 🤖 Categorizes an issue or PR as relevant to the Android platform

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Android] SpeechRecognizer cannot recognize again after StopRecognitionAsync

2 participants