开始
复制一个设置提示,包含安装步骤和该插件的完整Markdown指南。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-speech-recognition`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/speech-recognition/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
安装
标题为“安装”您可以使用我们的AI辅助设置来安装插件。将Capgo技能添加到您的AI工具中,使用以下命令:
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins然后使用以下提示:
Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-speech-recognition` plugin in my project.如果您更喜欢手动设置,请运行以下命令安装插件,并按照以下平台特定的说明进行操作:
bun add @capgo/capacitor-speech-recognitionbunx cap sync导入
导入import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';API概述
API 简介available
可用检查本机语音识别服务是否在当前设备上可用
import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
await SpeechRecognition.available();isOnDeviceRecognitionAvailable
标题:"isOnDeviceRecognitionAvailable"检查是否支持当前设备、操作系统版本和语言环境的设备端语音识别功能。
在启用设备端语音识别功能之前,应使用此功能进行能力检查。 useOnDeviceRecognition.
A true 返回true
Returns false 平台__CAPGO_KEEP_0__文档:
平台SDK文档: * iOS: 语音 Android: 语音识别器
import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
await SpeechRecognition.isOnDeviceRecognitionAvailable();start
标题:开始开始捕获音频并转录语音。
当 partialResults 是 true, 返回的 promise 立即解析并通过 partialResults 监听器将更新流式传输到会话结束。
默认路径保留了旧的识别器行为以保持向后兼容性。请在检查后 useOnDeviceRecognition: true 标题:停止
import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
await SpeechRecognition.start();stop
停止监听并释放本机资源。__CAPGO_KEEP_0__
import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
await SpeechRecognition.stop();forceStop
标题:forceStop强制停止当前会话。
在 Android 上,这首先尝试正常停止,然后回退到销毁/重新创建。 timeout在 iOS 上,当前会话立即停止。
如果有部分转录缓存,会通过 partialResults listener forced: true.
import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
await SpeechRecognition.forceStop();getLastPartialResult
标题:getLastPartialResult获取最后缓存的部分转录结果。
import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
await SpeechRecognition.getLastPartialResult();setPTTState
标题:setPTTState更新当前的按住说话按钮状态。
与此同时使用 continuousPTT 或与自定义按住说话流程一起使用。
import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
await SpeechRecognition.setPTTState({} as PTTStateOptions);getSupportedLanguages
标题为“getSupportedLanguages”获取底层识别器支持的语言列表。
Android 13+ 设备不再暴露此列表;在这种情况下 languages 是空的。
import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
await SpeechRecognition.getSupportedLanguages();isListening
标题为“isListening”返回插件是否正在积极监听语音输入
import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
await SpeechRecognition.isListening();checkPermissions
检查权限获取当前权限状态。
import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
await SpeechRecognition.checkPermissions();requestPermissions
requestPermissions请求麦克风 + 语音识别权限。
import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
await SpeechRecognition.requestPermissions();类型参考
类型参考SpeechRecognitionAvailability
语音识别可用性export interface SpeechRecognitionAvailability { available: boolean;}SpeechRecognitionStartOptions
SpeechRecognitionStartOptions配置识别器在调用 . 时的行为。
export interface SpeechRecognitionStartOptions { /** * Locale identifier such as `en-US`. When omitted the device language is used. */ language?: string; /** * Maximum number of final matches returned by native APIs. Defaults to `5`. */ maxResults?: number; /** * Prompt message shown inside the Android system dialog (ignored on iOS). */ prompt?: string; /** * When `true`, Android shows the OS speech dialog instead of running inline recognition. * Defaults to `false`. */ popup?: boolean; /** * Emits partial transcription updates through the `partialResults` listener while audio is captured. */ partialResults?: boolean; /** * Enables native punctuation handling where supported (iOS 16+). */ addPunctuation?: boolean; /** * Opt in to the platform's newer on-device recognition path when available. * * On iOS 26+, this uses Apple's `SpeechAnalyzer` / `SpeechTranscriber` pipeline. * On recent Android versions, this uses the on-device `SpeechRecognizer` path. * * It is intentionally opt-in so existing apps keep the legacy flow unless they choose * to roll out the new behavior. * * Use {@link SpeechRecognitionPlugin.isOnDeviceRecognitionAvailable} before enabling it in production. * * Platform SDK docs: * iOS: [Speech](https://developer.apple.com/documentation/speech), * [SpeechAnalyzer](https://developer.apple.com/documentation/speech/speechanalyzer), * [SpeechTranscriber](https://developer.apple.com/documentation/speech/speechtranscriber) * Android: [SpeechRecognizer](https://developer.android.com/reference/android/speech/SpeechRecognizer) * * Defaults to `false`. */ useOnDeviceRecognition?: boolean; /** * Allow a number of milliseconds of silence before splitting the recognition session into segments. * Required to be greater than zero and currently supported on Android only. */ allowForSilence?: number; /** * EXPERIMENTAL: Keep a PTT session alive across silence by restarting recognition while the button stays held. * * This restart behavior is implemented for Android inline recognition and iOS native recognition. */ continuousPTT?: boolean;}SpeechRecognitionMatches
SpeechRecognitionMatchesexport interface SpeechRecognitionMatches { matches?: string[];}ForceStopOptions
强制停止选项的选项
export interface ForceStopOptions { /** * Android only: timeout in milliseconds before forcing stop via destroy/recreate. * * On iOS, the current session is stopped immediately and this value is ignored. * * Defaults to `1500`. */ timeout?: number;}LastPartialResult
最后部分结果的结果
export interface LastPartialResult { /** * Whether a partial result is currently cached. */ available: boolean; /** * The most recent transcript text known to the native recognizer. */ text: string; /** * All current match alternatives when available. */ matches?: string[];}PTTStateOptions
PTT状态选项的选项
export interface PTTStateOptions { /** * Whether the PTT button is currently held. */ held: boolean;}SpeechRecognitionLanguages
语音识别语言export interface SpeechRecognitionLanguages { languages: string[];}SpeechRecognitionListening
语音识别监听export interface SpeechRecognitionListening { listening: boolean;}SpeechRecognitionPermissionStatus
语音识别权限状态权限映射由 checkPermissions 和 requestPermissions.
export interface SpeechRecognitionPermissionStatus { speechRecognition: PermissionState;}SpeechRecognitionSegmentResultEvent
语音识别分段结果事件仅在 Android 上产生的分段结果事件
export interface SpeechRecognitionSegmentResultEvent { matches: string[];}SpeechRecognitionPartialResultEvent
语音识别部分结果事件在产生部分转录时总是会触发。
export interface SpeechRecognitionPartialResultEvent { /** * Current recognition matches when the native recognizer reports them. * * This can be omitted for forced or accumulated-only payloads. */ matches?: string[]; /** * Accumulated transcription from earlier continuous PTT cycles. */ accumulated?: string; /** * Final accumulated text including the current result. */ accumulatedText?: string; /** * `true` when the plugin is restarting recognition inside a continuous PTT session. */ isRestarting?: boolean; /** * `true` when the payload was emitted by `forceStop()`. */ forced?: boolean;}SpeechRecognitionListeningEvent
标题为“SpeechRecognitionListeningEvent”当监听状态发生变化时会触发。
export interface SpeechRecognitionListeningEvent { /** * Finite state of the recognition session. */ state?: ListeningFiniteState; /** * Unique identifier for the current listening session. */ sessionId?: number; /** * Why this state transition occurred. */ reason?: ListeningReason; /** * Error code when the transition is caused by an error. */ errorCode?: string; /** * Backward-compatible binary state used by earlier releases. */ status?: 'started' | 'stopped';}真实来源
标题为“真实来源”本页面是由插件的 src/definitions.ts当公共API在上游发生变化时,请重新同步。
继续从开始
标题为“继续从开始”如果您正在使用 开始使用 为计划仪表板和API操作,连接它 使用@capgo/capacitor语音识别 为语音识别能力在使用@capgo/capacitor语音识别 API概述 为语音识别能力在API概述 介绍 为语音识别能力在介绍 API密钥 为语音识别能力在API密钥 设备 为语音识别能力在设备