one-hook
Size
0.58 kb
View source

useSpeechSynthesis

Web Speech API synthesis, reactified.

Installation

npm install @1hook/use-speech-synthesis

Usage

import {  } from '@1hook/use-speech-synthesis'

Basic text-to-speech:

const {  } = ()

('Hello, world!')

Configure speech options:

const {  } = ({
  : 'fr-FR',
  : 1.5,
  : 0.8,
  : 0.7,
})

('Bonjour le monde!')

Cancel speaking and check the current state:

const { , ,  } = ()

.() // state === 'idle'
('Hello, world!')
.() // state === 'speaking'
()
.() // state === 'idle'

API Reference

UseSpeechSynthesisOptions

The options for the useSpeechSynthesis hook. See MDN docs for more details.

PropTypeDefault
volume?
number
-
pitch?
number
-
rate?
number
-
voice?
SpeechSynthesisVoice | null
-
lang?
string
-

UseSpeechSynthesisReturn

The return value of the useSpeechSynthesis hook.

PropTypeDefault
cancel
() => void
-
speak
(text: string) => void
-
error
SpeechSynthesisErrorEvent | null
-
state
"idle" | "speaking" | "error"
-
utterance
SpeechSynthesisUtterance | null
-

On this page