语音合成(也被称作文本转语音或 TTS)包括接收 app 中需要语音合成的文本,再在设备扬声器或音频输出连接中播放出来这两个过程。
Web Speech API 对此有一个主要控制接口——SpeechSynthesis,外加一些处理如何表示要被合成的文本 (也被称为 utterances),用什么声音来播出 utterances 等工作的相关接口。同样的,许多操作系统都有自己的某种语音合成系统,在这个任务中我们调用可用的 API 来使用语音合成系统。
if ('speechSynthesis' in window) {
const u = new SpeechSynthesisUtterance('你好,世界');
u.lang = 'zh-CN';
speechSynthesis.speak(u);
}
if ('speechSynthesis' in window) {
function loopSpeak() {
speechSynthesis.cancel(); // 清队列
const u = new SpeechSynthesisUtterance(
'订单即将到期,请加急处理!'
);
u.lang = 'zh-CN';
u.rate = 0.9;
u.volume = 1;
u.onend = () => {
setTimeout(loopSpeak, 300); // 避免过快循环
};
speechSynthesis.speak(u);
}
loopSpeak();
}
SpeechSynthesis常用方法
1)speak(utterance)
将一个 SpeechSynthesisUtterance 添加到播放队列并朗读。
speechSynthesis.speak(u);
2)cancel()
清空所有队列,停止当前播放。 (最常用,因为可以避免队列堆积)
speechSynthesis.cancel();
3)pause()
暂停当前播放。
speechSynthesis.pause();
4)resume()
恢复暂停的播放。
speechSynthesis.resume();
5)getVoices()
返回可用的语音列表(不同语言、不同音色)。 注意:第一次调用可能返回空数组,需监听事件。
const voices = speechSynthesis.getVoices();
SpeechSynthesisUtterance实例属性
| 属性 | 说明 |
|---|---|
| text | 要朗读的文本 |
| lang | 语言(如 zh-CN, en-US) |
| voice | 指定具体 voice(从 getVoices() 中选) |
| volume | 音量(0~1) |
| rate | 语速(0.1~10)常用 0.8–1.2 |
| pitch | 音调(0~2)默认 1 |
| utterance.onend | 播放结束事件 |
| utterance.onerror | 播放失败事件 |
🔗 https://developer.mozilla.org/zh-CN/docs/Web/API/SpeechSynthesis