Vize

use-speech-synthesis

Speak text with the Web Speech API.

Package @vizejs/composable/use-speech-synthesis
Own the source vize lib pull composable:use-speech-synthesis
Runtime exports useSpeechSynthesis
Gzip budget 2560 B

Minimal setup

Use a Vue 3.5+ project with Vue SFC compilation configured (see Vite+ integration).

vp install @vizejs/composable

Import the entry directly. Call lifecycle-bound utilities inside <script setup> so cleanup follows the component scope; consult the runtime contract for this entry's SSR and hydration behavior.

import { useSpeechSynthesis } from "@vizejs/composable/use-speech-synthesis";

Try it

Read an announcement aloud

Speak text with the system voice and control playback.

Open the interactive example

What to observe: Read aloud speaks the announcement; Stop cancels it. The status shows whether speech is playing.

voices lists the installed voices; lang, rate and pitch can be refs. Every example renders a neutral message until it mounts, so server and client markup match, and then explains what to do when the browser lacks the API.

Compare the initial and interacted states
Read an announcement aloud: initial state
Initial state
Read an announcement aloud: state after the verified interactions
After the verified interactions

Copy the example

Save this complete SFC in your Vue project. Its public imports and source are the same ones executed in the preview.

<script setup lang="ts">
import { ref } from "vue";
import { useMounted } from "@vizejs/composable/use-mounted";
import { useSpeechSynthesis } from "@vizejs/composable/use-speech-synthesis";

const mounted = useMounted();
const text = ref("Welcome to Vue Fes Japan.");
const { supported, status, speak, cancel } = useSpeechSynthesis(text, { lang: "en-US", rate: 1 });
</script>

<template>
  <div class="composable-example">
    <p>Read an announcement aloud with the system voice.</p>
    <label>
      Announcement
      <input v-model="text" />
    </label>
    <div class="example-actions">
      <button type="button" :disabled="!mounted || !supported" @click="() => speak()">
        Read aloud
      </button>
      <button type="button" :disabled="!mounted || !supported" @click="() => cancel()">Stop</button>
    </div>
    <output aria-live="polite">{{
      !mounted
        ? "Checking browser support…"
        : supported
          ? `Speech: ${status}`
          : "Speech synthesis is not available in this browser."
    }}</output>
  </div>
</template>

Runtime contract

Utility Category Stability SSR Hydration Cleanup Targets Host globals Uses
useSpeechSynthesis media experimental deterministic-fallback caller-managed caller, reactive-scope web, desktop window tryOnScopeDispose

API

useSpeechSynthesis

Speak text with the Web Speech API. Each speak creates a fresh utterance from the current reactive text, language, voice, pitch, rate, and volume (validated ranges), and cancels speech previously started by this composable. status follows the utterance lifecycle; interruptions caused by cancel return to "idle" instead of reporting an error. The available voices are kept current via voiceschanged. Speech started by this composable is cancelled and the listener removed when the owning reactive scope stops. Server rendering: nothing is spoken; supported is false, status is "idle", and voices is empty.

function useSpeechSynthesis( text: MaybeRefOrGetter<string>, options: UseSpeechSynthesisOptions = {}, ): SpeechSynthesisControls
const text = ref("Hello, Vize");
const { speak, status } = useSpeechSynthesis(text, { rate: 1.2 });
speak();

Types

SpeechSynthesisVoiceLike

Minimal SpeechSynthesisVoice.

Member Type Description
name string Human-readable voice name.
lang string BCP 47 language tag.
voiceURI string Stable voice identifier.
localService boolean Whether synthesis runs locally.
default boolean Whether this is the platform default voice.

SpeechSynthesisUtteranceLike

Minimal SpeechSynthesisUtterance.

Member Type Description
text string Text to speak.
lang string BCP 47 language tag.
pitch number Pitch from 0 to 2.
rate number Rate from 0.1 to 10.
volume number Volume from 0 to 1.
voice SpeechSynthesisVoiceLike | null Voice, or null for the platform default.

SpeechSynthesisLike

Minimal speechSynthesis controller.

Member Type Description
speak (utterance: SpeechSynthesisUtteranceLike) => void Queue an utterance.
cancel () => void Clear the queue and stop speaking.
pause () => void Pause speaking.
resume () => void Resume speaking.
getVoices () => readonly SpeechSynthesisVoiceLike[] Voices available right now (may be empty until voiceschanged).

SpeechSynthesisHost

Capabilities used by useSpeechSynthesis.

Member Type Description
synthesis SpeechSynthesisLike Synthesis controller (window.speechSynthesis).
Utterance new (text?: string) => SpeechSynthesisUtteranceLike Utterance constructor (window.SpeechSynthesisUtterance).

UseSpeechSynthesisOptions

Options for useSpeechSynthesis.

Member Type Description
lang? MaybeRefOrGetter<string> BCP 47 language tag.
pitch? MaybeRefOrGetter<number> Pitch from 0 to 2.
rate? MaybeRefOrGetter<number> Rate from 0.1 to 10.
volume? MaybeRefOrGetter<number> Volume from 0 to 1.
voice? MaybeRefOrGetter<SpeechSynthesisVoiceLike | undefined> Voice to use; undefined uses the platform default for lang.
host? MaybeRefOrGetter<SpeechSynthesisHost | null | undefined> Synthesis capabilities for alternate runtimes and tests.

SpeechSynthesisControls

Reactive state and actions returned by useSpeechSynthesis.

Member Type Description
supported ComputedRef<boolean> Whether speech synthesis is available.
status Readonly<Ref<SpeechSynthesisStatus>> Playback state of the utterance spoken by this composable.
voices Readonly<ShallowRef<readonly SpeechSynthesisVoiceLike[]>> Voices offered by the platform, updated on voiceschanged.
error Readonly<ShallowRef<SpeechSynthesisErrorCode | undefined>> Most recent synthesis error, cleared by the next speak.
speak () => boolean Speak the current text, cancelling anything this composable is speaking.
pause () => void Pause speaking.
resume () => void Resume paused speech.
cancel () => void Stop speaking and clear the queue.