useSpeechSynthesis
响应式语音合成。
用法
import { useSpeechSynthesis } from '@vueuse/core'
const {
isSupported, // 是否支持
isPlaying, // 是否正在播放
status, // 状态
voiceInfo, // 语音信息
utterance, // 语音合成实例
error, // 错误信息
stop, // 停止播放
toggle, // 切换播放/暂停
speak, // 开始播放
} = useSpeechSynthesis()
选项
以下显示了选项的默认值,它们将直接传递给 SpeechSynthesis API。
useSpeechSynthesis({
lang: 'en-US', // 语言
pitch: 1, // 音高
rate: 1, // 语速
volume: 1, // 音量
})
类型声明
export type UseSpeechSynthesisStatus = "init" | "play" | "pause" | "end"
export interface UseSpeechSynthesisOptions extends ConfigurableWindow {
/**
* Language for SpeechSynthesis
*
* @default 'en-US'
*/
lang?: MaybeRefOrGetter<string>
/**
* Gets and sets the pitch at which the utterance will be spoken at.
*
* @default 1
*/
pitch?: MaybeRefOrGetter<SpeechSynthesisUtterance["pitch"]>
/**
* Gets and sets the speed at which the utterance will be spoken at.
*
* @default 1
*/
rate?: MaybeRefOrGetter<SpeechSynthesisUtterance["rate"]>
/**
* Gets and sets the voice that will be used to speak the utterance.
*/
voice?: MaybeRef<SpeechSynthesisVoice>
/**
* Gets and sets the volume that the utterance will be spoken at.
*
* @default 1
*/
volume?: MaybeRefOrGetter<SpeechSynthesisUtterance["volume"]>
}
/**
* Reactive SpeechSynthesis.
*
* @see https://vueuse.org/useSpeechSynthesis
* @see https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesis SpeechSynthesis
*/
export declare function useSpeechSynthesis(
text: MaybeRefOrGetter<string>,
options?: UseSpeechSynthesisOptions,
): {
isSupported: ComputedRef<boolean>
isPlaying: ShallowRef<boolean, boolean>
status: ShallowRef<UseSpeechSynthesisStatus, UseSpeechSynthesisStatus>
utterance: ComputedRef<SpeechSynthesisUtterance>
error: ShallowRef<
SpeechSynthesisErrorEvent | undefined,
SpeechSynthesisErrorEvent | undefined
>
stop: () => void
toggle: (value?: boolean) => void
speak: () => void
}
export type UseSpeechSynthesisReturn = ReturnType<typeof useSpeechSynthesis>