跳到内容

useSpeechRecognition

类别
导出大小
705 B
最后更改
5 天前

响应式 SpeechRecognition

我可以使用吗?

演示

您的浏览器不支持 SpeechRecognition API,更多详情

用法

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

const {
  isSupported,
  isListening,
  isFinal,
  result,
  start,
  stop,
} = useSpeechRecognition()

选项

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

ts
useSpeechRecognition({
  lang: 'en-US',
  interimResults: true,
  continuous: true,
})

类型声明

显示类型声明
typescript
export interface UseSpeechRecognitionOptions extends ConfigurableWindow {
  /**
   * Controls whether continuous results are returned for each recognition, or only a single result.
   *
   * @default true
   */
  continuous?: boolean
  /**
   * Controls whether interim results should be returned (true) or not (false.) Interim results are results that are not yet final
   *
   * @default true
   */
  interimResults?: boolean
  /**
   * Language for SpeechRecognition
   *
   * @default 'en-US'
   */
  lang?: MaybeRefOrGetter<string>
  /**
   * A number representing the maximum returned alternatives for each result.
   *
   * @see https://mdn.org.cn/en-US/docs/Web/API/SpeechRecognition/maxAlternatives
   * @default 1
   */
  maxAlternatives?: number
}
/**
 * Reactive SpeechRecognition.
 *
 * @see https://vueuse.org.cn/useSpeechRecognition
 * @see https://mdn.org.cn/en-US/docs/Web/API/SpeechRecognition SpeechRecognition
 * @param options
 */
export declare function useSpeechRecognition(
  options?: UseSpeechRecognitionOptions,
): {
  isSupported: ComputedRef<boolean>
  isListening: ShallowRef<boolean, boolean>
  isFinal: ShallowRef<boolean, boolean>
  recognition: SpeechRecognition | undefined
  result: ShallowRef<string, string>
  error: ShallowRef<
    SpeechRecognitionErrorEvent | undefined,
    SpeechRecognitionErrorEvent | undefined
  >
  toggle: (value?: boolean) => void
  start: () => void
  stop: () => void
}
export type UseSpeechRecognitionReturn = ReturnType<typeof useSpeechRecognition>

源码

源码演示文档

贡献者

Anthony Fu
IlyaL
Anthony Fu
Jelf
Byron
青椒肉丝
Neil Richter
Mark Noonan
vaakian X
wheat
Joe Maylor
Shinigami
Alex Kozack
EGGSY
Antério Vieira

更新日志

v12.8.0 on 2025/3/5
7432f - feat(types): 弃用 MaybeRefMaybeRefOrGetter,支持 Vue 原生 (#4636)
v12.6.0 on 2025/2/14
4f104 - fix: 改进 start 和 stop 方法的行为 (#4565)
v12.3.0 on 2025/1/2
890ab - fix: 在未结束时执行 'start' (#4119)
59f75 - feat(toValue): 弃用来自 @vueuse/sharedtoValue,支持 Vue 原生
v12.0.0-beta.1 on 2024/11/21
0a9ed - feat!: 移除 Vue 2 支持,优化 bundles 并清理代码 (#4349)
v11.0.0-beta.2 on 2024/7/17
9eda8 - feat: 支持 maxAlternatives 选项 (#4067)
v10.10.1 on 2024/6/11
7c36f - fix: 发送正确的 transcript 结果 (#3891)
v10.8.0 on 2024/2/20
a086e - fix: 更严格的类型
v10.0.0-beta.4 on 2023/4/13
4d757 - feat(types)!: 将 MaybeComputedRef 重命名为 MaybeRefOrGetter
10e98 - feat(toRef)!: 将 resolveRef 重命名为 toRef

在 MIT 许可证下发布。