跳过内容

开始

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.

如果您prefer手动设置,安装插件并运行以下命令并遵循以下平台特定说明:

终端窗口
npm install @capgo/capacitor-speech-recognition
npx cap sync

导入

导入
import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';

快速入门

快速入门

请求麦克风,附加一个监听器,然后开始一个带有实时更新的会话。 partialResults 常见问题

import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
await SpeechRecognition.requestPermissions();
const partialListener = await SpeechRecognition.addListener('partialResults', ({ matches }) => {
setTranscript(matches?.[0] ?? '');
});
await SpeechRecognition.start({ language: 'en-US', partialResults: true });
// When leaving the screen:
await SpeechRecognition.stop();
await partialListener.remove();

常见问题

清理
  • 导入:移除离开屏幕时的监听器。
  • 区域设置:使用与 UI 匹配的 BCP 47 标签。
  • Web:不受支持;如果您需要 Web-only 词典,请使用单独的浏览器语音API。

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

import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
const result = await SpeechRecognition.available();
console.log(result);

isOnDeviceRecognitionAvailable

设备上语音识别是否可用

标题:isOnDeviceRecognitionAvailable

您应该在启用之前使用此功能检查能力。 useOnDeviceRecognition使用此能力检查前应启用 true . A

. A false 结果表示当前设备、操作系统版本和语言环境可以使用该平台的新设备路径。

平台SDK文档: * iOS: Speech Android: iOS:

import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
const result = await SpeechRecognition.isOnDeviceRecognitionAvailable();
console.log(result);

语音识别器

When partialResults is true实时语音识别更新将通过实时更新流传输。 partialResults listener until the session ends.

立即解析并通过 useOnDeviceRecognition: true 监听器

import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
const result = await SpeechRecognition.start();
console.log(result);

仅在检查后传递。

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

停止监听并释放本机资源。

在 Android 上,这首先尝试正常停止,然后 fallback 到销毁/重新创建后 timeout。 在 iOS 上,当前会话立即停止。

如果缓存了部分转录结果,它将通过 partialResults listener forced: true.

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

getLastPartialResult

复制到剪贴板

获取最后缓存的部分语音识别结果。

import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
const result = await SpeechRecognition.getLastPartialResult();
console.log(result);

获取最后缓存的部分转录结果。

标题为“setPTTState” continuousPTT 更新当前推送到谈按钮状态。

import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
await SpeechRecognition.setPTTState({ held: true });

getSupportedLanguages

支持的语言

获取底层识别器支持的语言列表。

Android 13+ 设备不再暴露此列表;在这种情况下 languages 正在监听

import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
const result = await SpeechRecognition.getSupportedLanguages();
console.log(result);

正在监听

import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
const result = await SpeechRecognition.isListening();
console.log(result);

获取当前权限状态。

import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
const result = await SpeechRecognition.checkPermissions();
console.log(result);

requestPermissions

requestPermissions

请求麦克风 + 语音识别权限

import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
const result = await SpeechRecognition.requestPermissions();
console.log(result);

上下文:Capgo营销网站。角色: UI标签。位置:页面代理/[slug].astro。消息键`agents_page_copy_label` (代理页面复制标签)。

类型参考

SpeechRecognitionAvailability

Section titled “Type Reference”
export interface SpeechRecognitionAvailability {
available: boolean;
}

Section titled “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

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

PTTStateOptions

的选项

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

语音识别正在监听事件

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

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发生变化时重新同步。

继续从开始

继续从开始

如果您正在使用 开始 为计划仪表板和API操作连接它 使用@capgo/capacitor-speech-recognition 为使用@capgo/capacitor语音识别能力 API概览 为API概览中的实现细节 介绍 为介绍中的实现细节 API密钥 为API密钥中的实现细节,以及 设备 为设备中的实现细节。