SpeechSynthesis
Text-to-speech from C#: enumerate the platform's voices, speak utterances with voice, rate, pitch and volume control, and pause, resume or cancel playback.
@inject Bit.Butil.SpeechSynthesis speechSynthesisMDN reference
IsSupported reports whether window.speechSynthesis exists. GetVoices returns every SpeechVoice the platform offers - name, BCP-47 language tag, whether it is the default for its language and whether synthesis runs locally or via a network service.
@inject Bit.Butil.SpeechSynthesis speechSynthesis
var isSupported = await speechSynthesis.IsSupported();
SpeechVoice[] voices = await speechSynthesis.GetVoices();
foreach (var voice in voices)
{
// voice.Name, voice.Lang, voice.Default, voice.LocalService, voice.VoiceUri
}Configure a SpeechUtterance - text, an optional voice from GetVoices, rate (0.1–10; the slider below covers a practical 0.1–4), pitch (0–2) and volume (0–1) - and hand it to Speak. The call resolves once the engine accepts the utterance, not when speech finishes; utterances queue if you call Speak repeatedly. A string-only overload speaks with all defaults.
await speechSynthesis.Speak(new SpeechUtterance
{
Text = "Hello from Bit.Butil!",
VoiceName = "Microsoft Aria Online (Natural) - English (United States)",
Rate = 1.0,
Pitch = 1.0,
Volume = 1.0,
});
// or with defaults for everything:
await speechSynthesis.Speak("Hello from Bit.Butil!");Pause freezes the current utterance mid-word and Resume picks it back up. Cancel flushes the whole queue and stops any current speech - the right call when the user navigates away or starts a new narration.
await speechSynthesis.Pause();
await speechSynthesis.Resume();
await speechSynthesis.Cancel(); // flush the queue and stopIsSpeaking is true while an utterance is being spoken (including while paused); IsPending is true while at least one utterance is still waiting in the queue. Useful for toggling a speaking indicator or debouncing repeated Speak calls.
var speaking = await speechSynthesis.IsSpeaking();
var pending = await speechSynthesis.IsPending();API reference
ValueTask<bool> IsSupported()ValueTask<SpeechVoice[]> GetVoices()ValueTask Speak(SpeechUtterance utterance)ValueTask Speak(string text)ValueTask Cancel()ValueTask Pause()ValueTask Resume()ValueTask<bool> IsSpeaking()ValueTask<bool> IsPending()ValueTask<bool> IsPaused()