跳到主要內容

useSpeechSynthesis

分類
匯出大小
748 B
最後變更
5 天前

響應式 SpeechSynthesis

我可以使用嗎?

示範




用法

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 {
  /**
   * 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?: SpeechSynthesisUtterance["volume"]
}
/**
 * Reactive SpeechSynthesis.
 *
 * @see https://vueuse.dev.org.tw/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>

原始碼

原始碼示範文件

貢獻者

Anthony Fu
IlyaL
Anthony Fu
sibbng
Jelf
wheat
Kirill Shestmintsev
糠帥傅
zwkkwz1
云游君

更新日誌

v12.8.0 於 3/5/2025
7432f - feat(types): 棄用 MaybeRef 和 MaybeRefOrGetter,改用 Vue 原生語法 (#4636)
v12.3.0 於 1/2/2025
59f75 - feat(toValue): 棄用 @vueuse/shared 中的 toValue,改用 Vue 原生語法
v12.0.0-beta.1 於 11/21/2024
0a9ed - feat!: 移除 Vue 2 支援,優化捆綁包並清理 (#4349)
v10.3.0 於 7/30/2023
ac88f - feat: 為音調和速率新增響應式 (#3205)
v10.0.0-beta.4 於 4/13/2023
4d757 - feat(types)!: 將 MaybeComputedRef 重新命名為 MaybeRefOrGetter
10e98 - feat(toRef)!: 將 resolveRef 重新命名為 toRef
v9.13.0 於 2/18/2023
820e2 - feat: 新增 stop 函式 (#2774)

以 MIT 授權條款發布。