跳过内容

开始入门

GitHub

您可以使用我们的 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-recognition
bunx cap sync
import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';

检查当前设备上的本机语音识别服务是否可用。

import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
await SpeechRecognition.available();

isOnDeviceRecognitionAvailable

检测设备是否支持本地语音识别

检查设备是否支持本地语音识别

在启用本地语音识别之前,请使用此功能进行能力检查 useOnDeviceRecognition当设备仅支持旧版语音识别器时返回 true iOS:

语音 false Android:

Platform SDK docs: iOS: 复制到剪贴板 __CAPGO_KEEP_0__ 平台文档

import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
await SpeechRecognition.isOnDeviceRecognitionAvailable();

开始录音并转换语音。

partialResults is true, 返回的 promise 立即解析并通过 partialResults listener

更新流式传输,直到会话结束。 useOnDeviceRecognition: true 默认路径保留了遗留识别器行为以保证向后兼容性。

import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
await SpeechRecognition.start();

Copy to clipboard

import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
await SpeechRecognition.stop();

强制停止当前会话。

在 Android 上,这首先尝试正常停止,然后 fallback 到 destroy/recreate 后 timeout

在 iOS 上,当前会话立即停止。 partialResults 如果有部分转录缓存,则通过 forced: true.

import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
await SpeechRecognition.forceStop();

getLastPartialResult

复制到剪贴板

Section titled “getLastPartialResult”

import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
await SpeechRecognition.getLastPartialResult();

Section titled “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();

返回插件是否正在活跃地监听语音。

import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
await SpeechRecognition.isListening();

[__CAPGO_KEEP_0__]

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

《语音识别启动选项》

配置识别器在调用时的行为

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

语音识别匹配项
export 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

复制到剪贴板

语音识别匹配项

export interface PTTStateOptions {
/**
* Whether the PTT button is currently held.
*/
held: boolean;
}

SpeechRecognitionLanguages

强制停止选项
export interface SpeechRecognitionLanguages {
languages: string[];
}
export interface SpeechRecognitionListening {
listening: boolean;
}

checkPermissionsrequestPermissions.

export interface SpeechRecognitionPermissionStatus {
speechRecognition: PermissionState;
}

仅在Android上产生的分段结果时会引发。

export interface SpeechRecognitionSegmentResultEvent {
matches: string[];
}

产生部分转录时会引发。

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;
}

当监听状态发生变化时会触发。

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密钥的实现细节 设备 设备的实现细节