Lompat ke Konten

Getting Started

Jendela terminal
bun add @capgo/capacitor-speech-synthesis
bunx cap sync
import { SpeechSynthesis } from '@capgo/capacitor-speech-synthesis';

Membaca teks yang diberikan dengan pilihan tertentu. Utterance ditambahkan ke antrian pembicaraan.

import { SpeechSynthesis } from '@capgo/capacitor-speech-synthesis';
const result = await SpeechSynthesis.speak({
text: 'Hello, world!',
language: 'en-US',
rate: 1.0,
pitch: 1.0,
volume: 1.0,
queueStrategy: 'Add'
});
console.log('Utterance ID:', result.utteranceId);

Menghasilkan suara menjadi file audio (hanya Android/iOS). Mengembalikan jalur file tempat suara disimpan.

import { SpeechSynthesis } from '@capgo/capacitor-speech-synthesis';
const result = await SpeechSynthesis.synthesizeToFile({
text: 'Hello, world!',
language: 'en-US'
});
console.log('Audio file saved at:', result.filePath);

Membatalkan semua pernyataan yang ditunggu dan menghentikan suara saat ini.

import { SpeechSynthesis } from '@capgo/capacitor-speech-synthesis';
await SpeechSynthesis.cancel();

Menghentikan suara secara langsung.

import { SpeechSynthesis } from '@capgo/capacitor-speech-synthesis';
await SpeechSynthesis.pause();

Mengaktifkan kembali suara yang dihentikan.

import { SpeechSynthesis } from '@capgo/capacitor-speech-synthesis';
await SpeechSynthesis.resume();

Mengecek apakah sintesis suara sedang berbicara.

import { SpeechSynthesis } from '@capgo/capacitor-speech-synthesis';
const { isSpeaking } = await SpeechSynthesis.isSpeaking();
console.log('Is speaking:', isSpeaking);

Mengecek apakah sintesis suara tersedia di perangkat.

import { SpeechSynthesis } from '@capgo/capacitor-speech-synthesis';
const { isAvailable } = await SpeechSynthesis.isAvailable();
if (isAvailable) {
console.log('Speech synthesis is available');
}

Mengambil semua suara yang tersedia.

import { SpeechSynthesis } from '@capgo/capacitor-speech-synthesis';
const { voices } = await SpeechSynthesis.getVoices();
voices.forEach(voice => {
console.log(`${voice.name} (${voice.language})`);
});

Mengambil semua bahasa yang tersedia.

import { SpeechSynthesis } from '@capgo/capacitor-speech-synthesis';
const { languages } = await SpeechSynthesis.getLanguages();
console.log('Available languages:', languages);

Mengecek apakah bahasa tertentu tersedia.

import { SpeechSynthesis } from '@capgo/capacitor-speech-synthesis';
const { isAvailable } = await SpeechSynthesis.isLanguageAvailable({
language: 'es-ES'
});
console.log('Spanish available:', isAvailable);

Mengecek apakah suara tertentu tersedia.

import { SpeechSynthesis } from '@capgo/capacitor-speech-synthesis';
const { isAvailable } = await SpeechSynthesis.isVoiceAvailable({
voiceId: 'com.apple.ttsbundle.Samantha-compact'
});
console.log('Voice available:', isAvailable);

Mengaktifkan mesin sintesis suara (optimasi iOS). Hal ini dapat mengurangi latensi untuk permintaan suara pertama.

import { SpeechSynthesis } from '@capgo/capacitor-speech-synthesis';
await SpeechSynthesis.initialize();

Mengaktifkan sesi audio dengan kategori tertentu (hanya iOS).

import { SpeechSynthesis } from '@capgo/capacitor-speech-synthesis';
await SpeechSynthesis.activateAudioSession({
category: 'Playback'
});

Menghentikan sesi audio (hanya iOS).

import { SpeechSynthesis } from '@capgo/capacitor-speech-synthesis';
await SpeechSynthesis.deactivateAudioSession();

Opsi untuk berbicara teks.

export interface SpeakOptions {
/**
* The text to speak.
*
* @since 1.0.0
*/
text: string;
/**
* The BCP-47 language tag (e.g., 'en-US', 'es-ES').
*
* @since 1.0.0
*/
language?: string;
/**
* The voice identifier to use.
*
* @since 1.0.0
*/
voiceId?: string;
/**
* The pitch of the voice (0.5 to 2.0, default: 1.0).
*
* @since 1.0.0
*/
pitch?: number;
/**
* The speaking rate (0.1 to 10.0, default: 1.0).
*
* @since 1.0.0
*/
rate?: number;
/**
* The volume (0.0 to 1.0, default: 1.0).
*
* @since 1.0.0
*/
volume?: number;
/**
* The queue strategy: 'Add' to append or 'Flush' to replace queue.
* Default: 'Add'
*
* @since 1.0.0
*/
queueStrategy?: 'Add' | 'Flush';
}

Hasil dari berbicara teks.

export interface SpeakResult {
/**
* Unique identifier for this utterance.
*
* @since 1.0.0
*/
utteranceId: string;
}

Hasil sintesis ke file.

export interface SynthesizeToFileResult {
/**
* The file path where audio was saved.
*
* @since 1.0.0
*/
filePath: string;
/**
* Unique identifier for this utterance.
*
* @since 1.0.0
*/
utteranceId: string;
}

Informasi tentang suara.

export interface VoiceInfo {
/**
* Unique voice identifier.
*
* @since 1.0.0
*/
id: string;
/**
* Display name of the voice.
*
* @since 1.0.0
*/
name: string;
/**
* BCP-47 language code.
*
* @since 1.0.0
*/
language: string;
/**
* Gender of the voice (iOS only).
*
* @since 1.0.0
*/
gender?: 'male' | 'female' | 'neutral';
/**
* Whether this voice requires a network connection.
*
* @since 1.0.0
*/
isNetworkConnectionRequired?: boolean;
/**
* Whether this is the default voice (Web only).
*
* @since 1.0.0
*/
default?: boolean;
}

Opsi untuk memeriksa ketersediaan bahasa.

export interface IsLanguageAvailableOptions {
/**
* The BCP-47 language code to check.
*
* @since 1.0.0
*/
language: string;
}

Opsi untuk memeriksa ketersediaan suara.

export interface IsVoiceAvailableOptions {
/**
* The voice ID to check.
*
* @since 1.0.0
*/
voiceId: string;
}

Opsi untuk mengaktifkan sesi audio (hanya iOS).

export interface ActivateAudioSessionOptions {
/**
* The audio session category.
* - 'Ambient': Mixes with other audio
* - 'Playback': Stops other audio
*
* @since 1.0.0
*/
category: 'Ambient' | 'Playback';
}

Event yang dikeluarkan ketika ucapan dimulai atau berakhir.

export interface UtteranceEvent {
/**
* The utterance identifier.
*
* @since 1.0.0
*/
utteranceId: string;
}

Event yang dikeluarkan pada batas kata.

export interface BoundaryEvent {
/**
* The utterance identifier.
*
* @since 1.0.0
*/
utteranceId: string;
/**
* The character index in the text.
*
* @since 1.0.0
*/
charIndex: number;
/**
* The character length of the current word.
*
* @since 1.0.0
*/
charLength?: number;
}

Event yang dikeluarkan pada kesalahan sintesis.

export interface ErrorEvent {
/**
* The utterance identifier.
*
* @since 1.0.0
*/
utteranceId: string;
/**
* The error message.
*
* @since 1.0.0
*/
error: string;
}

Sumber Kebenaran

Sumber Kebenaran

Halaman ini dihasilkan dari plugin’s src/definitions.tsJalankan ulang sinkronisasi ketika publik API berubah di atas

Teruskan dari Getting Started

Teruskan dari Getting Started

Jika Anda menggunakan Getting Started untuk merencanakan dashboard dan API operasi, hubungkannya dengan Menggunakan @capgo/capacitor-speech-synthesis Menggunakan @capgo/capacitor-speech-synthesis Ringkasan API untuk detail implementasi di API Ringkasan, Pendahuluan untuk detail implementasi di Pendahuluan, API Kunci untuk detail implementasi di API Kunci, dan Perangkat untuk detail implementasi di Perangkat.