All skills
davidortinau avatar

/maui-speech-to-text

@74acbe3
by David Ortinaudavidortinau/maui-skills173 stars
20

Add speech-to-text voice input to .NET MAUI apps using CommunityToolkit.Maui. Covers speech recognition, microphone permissions, and hands-free text entry. Works with any UI pattern (XAML/MVVM, C# Markup, MauiReactor). USE FOR: "speech to text", "voice input", "speech recognition", "microphone input", "voice command", "dictation", "SpeechToText", "hands-free text", "transcribe speech". DO NOT USE FOR: text-to-speech output (different feature), media playback (use maui-media-picker), or general permissions (use maui-permissions).

Use this Skill: https://skilld.dev/gh/davidortinau/maui-skills/maui-speech-to-text

This session only. Nothing lands on disk.

SKILL.md

β‰ˆ139 tokens always: the name and description. β‰ˆ1.1k when used: this file. β‰ˆ3.2k more on demand in 1 file.

Speech-to-Text β€” Gotchas & Best Practices

For full service implementation, types, and UI integration patterns, see references/speech-to-text-api.md.

Critical: Permission Handling

Always request permissions before starting speech recognition. Both microphone and speech permissions are required.

// ❌ Starting recognition without checking permissions
var result = await _speechService.StartListeningAsync();

// βœ… Always check permissions first
if (!await _speechService.RequestPermissionsAsync())
    return; // Gracefully handle denial
var result = await _speechService.StartListeningAsync(cancellationToken);

⚠️ iOS Requires Both Permission Descriptions

Missing either NSSpeechRecognitionUsageDescription or NSMicrophoneUsageDescription in Info.plist will cause a runtime crash β€” not a graceful failure.

⚠️ Android: Permission Re-prompt

On Android, if the user denies the RECORD_AUDIO permission twice, the OS stops showing the prompt. You must guide users to Settings manually.

Common Mistakes

❌ No Timeout β€” Indefinite Listening

Always set a timeout to prevent indefinite listening sessions that drain battery:

// ❌ No timeout β€” listens forever if no speech detected
await _speechToText.StartListenAsync(options, CancellationToken.None);

// βœ… Use a combined timeout + user cancellation token
using var timeoutCts = new CancellationTokenSource(TimeSpan.FromSeconds(60));
using var combinedCts = CancellationTokenSource.CreateLinkedTokenSource(
    userCancellationToken, timeoutCts.Token);
await _speechToText.StartListenAsync(options, combinedCts.Token);

❌ Not Unsubscribing Event Handlers

Leaking event subscriptions causes duplicate processing and memory leaks:

// ❌ Subscribe without unsubscribe
_speechToText.RecognitionResultUpdated += OnRecognitionResultUpdated;

// βœ… Always unsubscribe in finally block
try
{
    _speechToText.RecognitionResultUpdated += OnRecognitionResultUpdated;
    // ... listen ...
}
finally
{
    _speechToText.RecognitionResultUpdated -= OnRecognitionResultUpdated;
}

❌ Not Disposing CancellationTokenSource

// ❌ Leaked CTS
_currentCts = new CancellationTokenSource();

// βœ… Dispose in finally
try { /* ... */ }
finally
{
    _currentCts?.Dispose();
    _currentCts = null;
}

Platform Pitfalls

Platform Pitfall
iOS Missing either plist key β†’ runtime crash
Android User denies permission twice β†’ OS stops prompting; must redirect to Settings
All No timeout β†’ battery drain from indefinite listening
All Calling StartListeningAsync while already listening β†’ returns error, not exception

Architecture Tips

  1. Wrap ISpeechToText in a service β€” Don't use SpeechToText.Default directly in ViewModels. Wrap in ISpeechRecognitionService for testability and state management.

  2. Use partial results for UX β€” Subscribe to PartialResultReceived for live transcription feedback. Users expect to see words appear as they speak.

  3. Continuous listening = loop with delay β€” Loop StartListeningAsync with small delays (Task.Delay(100)) for conversation mode.

  4. Guard against double-start β€” Check state before starting:

    if (State == SpeechRecognitionState.Listening)
        return new SpeechRecognitionResultDto { Success = false, ErrorMessage = "Already listening" };
  5. Natural language output β€” CommunityToolkit.Maui returns normalized, punctuated text β€” not raw phonemes. No post-processing needed for basic use cases.

  6. UI-agnostic service β€” The ISpeechRecognitionService pattern works identically with XAML/MVVM, C# Markup, and MauiReactor. See references/speech-to-text-api.md for all three patterns.

Checklist

  • CommunityToolkit.Maui NuGet installed (look up current version)
  • UseMauiCommunityToolkit() called in MauiProgram.cs
  • ISpeechToText registered as singleton via DI
  • iOS: Both NSSpeechRecognitionUsageDescription and NSMicrophoneUsageDescription in Info.plist
  • Android: RECORD_AUDIO permission in AndroidManifest.xml
  • Permissions checked before every StartListeningAsync call
  • Timeout configured (recommend 60 seconds max)
  • Event handlers unsubscribed in finally blocks
  • CancellationTokenSource disposed after use

Source: SKILL.md on GitHub

No alerts15d4 checks Β· Risk SAFE
  • Gen Agent Trust Hub15d

    This skill provides educational content and code implementation for adding speech-to-text capabilities to .NET MAUI applications using the official CommunityToolkit.Maui library. It covers best practices for permission handling, resource management, and UI integration. No malicious patterns or security risks were identified.

  • Socket15d

    No alerts

  • Snyk15d

    Risk: LOW Β· No issues

  • Runlayer7mo

    1 file scanned Β· No issues

Signed by skilld at 74acbe3. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Steadyupdated 6 months ago

README badge

README badge for davidortinau/maui-skills/maui-speech-to-text