Skip to content

useSpeechRecognition

Category
Export Size
694 B
Last Changed
15 minutes ago

响应式 SpeechRecognition

Can I use?

Demo

您的浏览器不支持语音识别 API, 查看更多详情

用法

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

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

选项

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

ts
useSpeechRecognition({
  lang: 'zh-CN',
  interimResults: true,
  continuous: true,
})

类型声明

显示类型声明
typescript
export interface UseSpeechRecognitionOptions extends ConfigurableWindow {
  /**
   * 控制是否对每次识别返回连续的结果,还是仅返回单个结果。
   *
   * @default true
   */
  continuous?: boolean
  /**
   * 控制是否返回临时结果(true)或不返回临时结果(false)。
   * 临时结果是尚未最终确定的结果。
   *
   * @default true
   */
  interimResults?: boolean
  /**
   * 语音识别的语言
   *
   * @default 'en-US'
   */
  lang?: MaybeRefOrGetter<string>
  /**
   * A number representing the maximum returned alternatives for each result.
   *
   * @see https://developer.mozilla.org/en-US/docs/Web/API/SpeechRecognition/maxAlternatives
   * @default 1
   */
  maxAlternatives?: number
}
/**
 * 响应式语音识别。
 *
 * @see https://vueuse.org/useSpeechRecognition
 * @see https://developer.mozilla.org/en-US/docs/Web/API/SpeechRecognition SpeechRecognition
 * @param options
 */
export declare function useSpeechRecognition(
  options?: UseSpeechRecognitionOptions,
): {
  isSupported: ComputedRef<boolean>
  isListening: Ref<boolean, boolean>
  isFinal: Ref<boolean, boolean>
  recognition: SpeechRecognition | undefined
  result: Ref<string, string>
  error: Ref<
    SpeechRecognitionErrorEvent | undefined,
    SpeechRecognitionErrorEvent | undefined
  >
  toggle: (value?: boolean) => void
  start: () => void
  stop: () => void
}
export type UseSpeechRecognitionReturn = ReturnType<typeof useSpeechRecognition>

Source

SourceDemoDocs

贡献者

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

变更日志

v11.0.0-beta.2 on 7/17/2024
9eda8 - feat: support maxAlternatives option (#4067)
v10.10.1 on 6/11/2024
7c36f - fix: send correct transcript result (#3891)
v10.8.0 on 2/20/2024
a086e - fix: stricter types
v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
10e98 - feat(toRef)!: rename resolveRef to toRef

Released under the MIT License.