Skip to content
On this page

useSpeechSynthesis

Category
Last Changed
3 months ago

Reactive SpeechSynthesis.

Can I use?

Demo

Your browser does not support SpeechSynthesis API, more details

Usage

import { useSpeechSynthesis } from '@vueuse/core'

const {
  isSupported,
  isPlaying,
  status,
  voiceInfo,
  utterance,
  error,

  toggle,
  speak
} = useSpeechSynthesis()
import { useSpeechSynthesis } from '@vueuse/core'

const {
  isSupported,
  isPlaying,
  status,
  voiceInfo,
  utterance,
  error,

  toggle,
  speak
} = useSpeechSynthesis()

Options

The following shows the default values of the options, they will be directly passed to SpeechSynthesis API.

{
  lang: 'en-US',
  pitch: 1,
  rate: 1,
  volume: 1,
}
{
  lang: 'en-US',
  pitch: 1,
  rate: 1,
  volume: 1,
}

Type Declarations

Show Type Declarations
export declare type Status = "init" | "play" | "pause" | "end"
export declare type VoiceInfo = Pick<SpeechSynthesisVoice, "lang" | "name">
export interface SpeechSynthesisOptions extends ConfigurableWindow {
  /**
   * Language for SpeechSynthesis
   *
   * @default 'en-US'
   */
  lang?: MaybeRef<string>
  /**
   * Gets and sets the pitch at which the utterance will be spoken at.
   *
   * @default 1
   */
  pitch?: SpeechSynthesisUtterance["pitch"]
  /**
   * Gets and sets the speed at which the utterance will be spoken at.
   *
   * @default 1
   */
  rate?: SpeechSynthesisUtterance["rate"]
  /**
   * Gets and sets the voice that will be used to speak the utterance.
   */
  voice?: SpeechSynthesisVoice
  /**
   * Gets and sets the volume that the utterance will be spoken at.
   *
   * @default 1
   */
  volume?: SpeechSynthesisUtterance["volume"]
}
/**
 * Reactive SpeechSynthesis.
 *
 * @see https://vueuse.org/useSpeechSynthesis
 * @see https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesis SpeechSynthesis
 * @param options
 */
export declare function useSpeechSynthesis(
  text: MaybeRef<string>,
  options?: SpeechSynthesisOptions
): {
  isSupported: boolean
  isPlaying: Ref<boolean>
  status: Ref<Status>
  voiceInfo: {
    lang: string
    name: string
  }
  utterance: ComputedRef<SpeechSynthesisUtterance>
  error: Ref<SpeechSynthesisErrorEvent | undefined>
  toggle: (value?: boolean) => void
  speak: () => void
}
export declare type UseSpeechSynthesisReturn = ReturnType<
  typeof useSpeechSynthesis
>
export declare type Status = "init" | "play" | "pause" | "end"
export declare type VoiceInfo = Pick<SpeechSynthesisVoice, "lang" | "name">
export interface SpeechSynthesisOptions extends ConfigurableWindow {
  /**
   * Language for SpeechSynthesis
   *
   * @default 'en-US'
   */
  lang?: MaybeRef<string>
  /**
   * Gets and sets the pitch at which the utterance will be spoken at.
   *
   * @default 1
   */
  pitch?: SpeechSynthesisUtterance["pitch"]
  /**
   * Gets and sets the speed at which the utterance will be spoken at.
   *
   * @default 1
   */
  rate?: SpeechSynthesisUtterance["rate"]
  /**
   * Gets and sets the voice that will be used to speak the utterance.
   */
  voice?: SpeechSynthesisVoice
  /**
   * Gets and sets the volume that the utterance will be spoken at.
   *
   * @default 1
   */
  volume?: SpeechSynthesisUtterance["volume"]
}
/**
 * Reactive SpeechSynthesis.
 *
 * @see https://vueuse.org/useSpeechSynthesis
 * @see https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesis SpeechSynthesis
 * @param options
 */
export declare function useSpeechSynthesis(
  text: MaybeRef<string>,
  options?: SpeechSynthesisOptions
): {
  isSupported: boolean
  isPlaying: Ref<boolean>
  status: Ref<Status>
  voiceInfo: {
    lang: string
    name: string
  }
  utterance: ComputedRef<SpeechSynthesisUtterance>
  error: Ref<SpeechSynthesisErrorEvent | undefined>
  toggle: (value?: boolean) => void
  speak: () => void
}
export declare type UseSpeechSynthesisReturn = ReturnType<
  typeof useSpeechSynthesis
>

Source

SourceDemoDocs

Contributors

Anthony Fu
wheat
云游君

Changelog

v6.6.0 on 10/16/2021
4dd83 - feat: new function (#837)
useSpeechSynthesis has loaded