Skip to content

useSpeechSynthesis

Category
Export Size
724 B
Last Changed
15 minutes ago

响应式 SpeechSynthesis

Can I use?

Demo




用法

ts
import { useSpeechSynthesis } from '@vueuse/core'

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

  toggle,
  speak,
} = useSpeechSynthesis()

选项

以下显示了选项的默认值,它们将直接传递给 SpeechSynthesis API

ts
useSpeechSynthesis({
  lang: 'en-US',
  pitch: 1,
  rate: 1,
  volume: 1,
})

类型声明

显示类型声明
typescript
export type UseSpeechSynthesisStatus = "init" | "play" | "pause" | "end"
export interface UseSpeechSynthesisOptions extends ConfigurableWindow {
  /**
   * 语音合成的语言
   *
   * @default 'en-US'
   */
  lang?: MaybeRefOrGetter<string>
  /**
   * 获取和设置朗读时的音调。
   *
   * @default 1
   */
  pitch?: MaybeRefOrGetter<SpeechSynthesisUtterance["pitch"]>
  /**
   * 获取和设置朗读时的速度。
   *
   * @default 1
   */
  rate?: MaybeRefOrGetter<SpeechSynthesisUtterance["rate"]>
  /**
   * 获取和设置用于朗读的声音。
   */
  voice?: MaybeRef<SpeechSynthesisVoice>
  /**
   * 获取和设置朗读的音量。
   *
   * @default 1
   */
  volume?: SpeechSynthesisUtterance["volume"]
}
/**
 * 响应式语音合成。
 *
 * @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: Ref<boolean, boolean>
  status: Ref<UseSpeechSynthesisStatus, UseSpeechSynthesisStatus>
  utterance: ComputedRef<SpeechSynthesisUtterance>
  error: Ref<
    SpeechSynthesisErrorEvent | undefined,
    SpeechSynthesisErrorEvent | undefined
  >
  stop: () => void
  toggle: (value?: boolean) => void
  speak: () => void
}
export type UseSpeechSynthesisReturn = ReturnType<typeof useSpeechSynthesis>

Source

SourceDemoDocs

贡献者

Anthony Fu
丶远方
sibbng
Anthony Fu
Jelf
wheat
Kirill Shestmintsev
糠帅傅
zwkkwz1
云游君

变更日志

v10.3.0 on 7/30/2023
ac88f - feat: add reactivity for pitch and rate (#3205)
v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
10e98 - feat(toRef)!: rename resolveRef to toRef
v9.13.0 on 2/18/2023
820e2 - feat: add stop function (#2774)

Released under the MIT License.