SpeechRecognition
Speech-to-text from C#: start a recognition session with language and interim-result options, stream transcripts into your components and stop or dispose the session when done.
@inject Bit.Butil.SpeechRecognition speechRecognitionMDN reference
Returns true when the runtime exposes a SpeechRecognition implementation - standard or webkit-prefixed. The Butil script resolves both, so you never have to feature-detect the prefix yourself.
@inject Bit.Butil.SpeechRecognition speechRecognition
var isSupported = await speechRecognition.IsSupported();Start begins listening and returns an IAsyncDisposable handle. Options select the language, whether recognition keeps running across pauses (Continuous), whether interim non-final transcripts are reported (InterimResults) and how many alternatives to surface. Each result arrives in the onResult callback as a SpeechRecognitionResult with Transcript, Confidence and IsFinal.
await using var session = await speechRecognition.Start(
new SpeechRecognitionOptions
{
Lang = "en-US",
Continuous = true,
InterimResults = true,
MaxAlternatives = 1,
},
onResult: result =>
{
// result.Transcript, result.Confidence (0-1), result.IsFinal
},
onError: message => { /* mic denied, no speech, network, ... */ },
onEnd: () => { /* engine stopped listening */ });Disposing the handle returned by Start stops that session and detaches its callbacks - an await using block covers the common case. The service itself is IAsyncDisposable too and tears down every live session when the scope ends, but pages that hold a handle across renders should dispose it explicitly in their own DisposeAsync.
IAsyncDisposable
{
private IAsyncDisposable? session;
private async Task Stop()
{
if (session is null) return;
await session.DisposeAsync(); // stops the engine and detaches callbacks
session = null;
}
public async ValueTask DisposeAsync() => await Stop();
}API reference
ValueTask<bool> IsSupported()Task<IAsyncDisposable> Start(SpeechRecognitionOptions options, Action<SpeechRecognitionResult>? onResult = null, Action<string>? onError = null, Action? onEnd = null)ValueTask Stop(Guid id)ValueTask DisposeAsync()class { string? Lang; bool Continuous; bool InterimResults = true; int MaxAlternatives = 1; }class { string Transcript; double Confidence; bool IsFinal; }void InvokeSpeechRecognition…(…)