跳过主要内容
返回插件
@capgo/capacitor-speech-synthesis
教程
由 github.com/Cap-go

语音合成

从文本中合成语音,完全控制语言、声音、音调、速度和音量。

指南

语音合成教程

使用@capgo/capacitor-语音合成

语音合成插件:将文本合成语音。

安装

bun add @capgo/capacitor-speech-synthesis
bunx cap sync

此插件暴露的内容

  • speak - 使用指定选项朗读给定的文本。该发言被添加到语音队列中。
  • synthesizeToFile - 将语音合成为音频文件(仅Android/iOS)。返回保存音频的文件路径。
  • cancel - 取消所有排队的发言并停止当前语音。
  • pause - 立即停止语音输出。

示例用法

speak

使用指定选项朗读给定的文本。该发言被添加到语音队列中。

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

synthesizeToFile

将语音合成为音频文件 (仅 Android/iOS)。返回保存音频的文件路径。

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

cancel

取消所有排队的发言并停止当前语音输出。

import { SpeechSynthesis } from '@capgo/capacitor-speech-synthesis';

await SpeechSynthesis.cancel();

pause

立即停止语音输出。

import { SpeechSynthesis } from '@capgo/capacitor-speech-synthesis';

await SpeechSynthesis.pause();

完整参考