Lebih Cepat ke Konten

Mulai Membuat

GitHub

Anda dapat menggunakan Setup Bantuan AI kami untuk menginstal plugin. Tambahkan Capgo kemampuan ke alat AI Anda menggunakan perintah berikut:

Jendela Terminal
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins

Lalu gunakan prompt berikut:

Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-speech-recognition` plugin in my project.

Jika Anda lebih suka Manual Setup, instal plugin dengan menjalankan perintah-perintah berikut dan ikuti instruksi spesifik platform di bawah ini:

Jendela Terminal
bun add @capgo/capacitor-speech-recognition
bunx cap sync
import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';

Mengecek apakah layanan pengenalan suara native dapat digunakan pada perangkat saat ini.

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

Mengecek apakah jalur pengenalan perangkat yang lebih baru di perangkat ini tersedia untuk lokasi yang dipilih.

Ini adalah pengecekan kemampuan yang harus digunakan sebelum mengaktifkan useOnDeviceRecognition. A true Hasilnya berarti perangkat, versi OS, dan lokasi saat ini dapat menggunakan jalur yang lebih baru untuk platform tersebut.

Mengembalikan false ketika perangkat hanya mendukung jalur recognizer legasi.

Dokumen SDK platform: iOS: Pengenalan Suara Android:

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

Mengambil audio dan menerjemahkan percakapan.

Ketika partialResults adalah true, maka promise yang dikembalikan segera diselesaikan dan perubahan streamed melalui partialResults pengguna sampai sesi berakhir.

Jalan default menjaga perilaku pengenalan warisan untuk konsistensi ke belakang. Lampirkan useOnDeviceRecognition: true hanya setelah memeriksa .

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

Menghentikan mendengarkan dan menurunkan sumber daya asli.

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

Menghentikan sesi saat ini secara paksa.

Pada Android, hal ini mencoba menghentikan secara normal terlebih dahulu, kemudian kembali ke destroy/recreate setelah itu. timeout. Pada iOS, sesi saat ini dihentikan langsung.

Jika hasil transkripsi parsial disimpan, maka hasil tersebut akan dikirim melalui partialResults dengan forced: true.

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

Mengambil hasil transkripsi parsial terakhir yang disimpan.

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

Mengupdate status tombol bicara push-to-talk saat ini.

Gunakan bersamaan dengan continuousPTT atau dengan alur tahan-tahan untuk berbicara.

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

Mendapatkan daftar bahasa yang didukung oleh pengenalan suara yang ada di bawahnya.

Perangkat Android 13+ tidak lagi menampilkan daftar ini; dalam hal itu languages kosong.

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

Mengembalikan apakah plugin aktif mendengarkan suara.

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

Mendapatkan status izin saat ini.

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

Minta izin mikrofon + pengenalan suara.

import { SpeechRecognition } from '@capgo/capacitor-speech-recognition';
await SpeechRecognition.requestPermissions();
export interface SpeechRecognitionAvailability {
available: boolean;
}

Konfigurasi cara pengenalan suara berperilaku ketika memanggil .

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;
}
export interface SpeechRecognitionMatches {
matches?: string[];
}

Opsi untuk .

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

Hasil dari .

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

Opsi untuk .

export interface PTTStateOptions {
/**
* Whether the PTT button is currently held.
*/
held: boolean;
}
export interface SpeechRecognitionLanguages {
languages: string[];
}
export interface SpeechRecognitionListening {
listening: boolean;
}

Peta Izin yang dikembalikan oleh checkPermissions dan requestPermissions.

export interface SpeechRecognitionPermissionStatus {
speechRecognition: PermissionState;
}

Ditinggalkan ketika hasil segmentasi diproduksi (hanya Android).

export interface SpeechRecognitionSegmentResultEvent {
matches: string[];
}

Ditinggalkan ketika transkripsi parsial diproduksi.

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

Ditimbulkan ketika status mendengarkan berubah.

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

Halaman ini dihasilkan dari plugin’s src/definitions.ts. Re-run sinkronisasi ketika publik API berubah di atas.

Jika Anda menggunakan Getting Started Untuk merencanakan dashboard dan API operasi, hubungkannya dengan Menggunakan @capgo/capacitor-pengenalan-suara Untuk kemampuan asli dalam Menggunakan @capgo/capacitor-pengenalan-suara, Ringkasan API Untuk detail implementasi dalam Ringkasan API, Pendahuluan Untuk detail implementasi dalam Pendahuluan, Kunci API Untuk detail implementasi dalam Kunci API, dan Perangkat Untuk detail implementasi dalam Perangkat.