mcpField
M
maui-speech-to-text

maui-speech-to-text

github.com/davidortinauDevelopment17642 installsUpdated 16w ago
Repository

About maui-speech-to-text

A speech-to-text implementation guide for .NET MAUI developers, covering permission, listening, and event-handling considerations for CommunityToolkit.Maui. It outlines platform pitfalls and service-wrapping practices.

Key features

  1. 01Check microphone and speech permissions first
  2. 02Set listening timeouts and cancellation tokens
  3. 03Unsubscribe event handlers reliably
  4. 04Show live transcription with partial results
  5. 05Wrap speech recognition in a service

Use cases

  1. 01Avoid iOS crashes from missing permission descriptions
  2. 02Handle Android microphone permission denial
  3. 03Prevent battery drain and duplicate event processing
  4. 04Display live transcription in a MAUI interface

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/davidortinau/maui-skills --skill maui-speech-to-text

After installing, type/maui-speech-to-textin Claude Code to confirm it loaded.

Bundled filesreferences/speech-to-text-api.md

Details

From the project README

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

PlatformPitfall
iOSMissing either plist key → runtime crash
AndroidUser denies permission twice → OS stops prompting; must redirect to Settings
AllNo timeout → battery drain from indefinite listening
AllCalling 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

Comments

Markdown supported