文本转语音

Web Speech API 有两个部分:SpeechSynthesis 语音合成 (文本到语音 TTS)和 SpeechRecognition  语音识别(异步语音识别)

复制代码
SpeechSynthesis.cancel():移除所有语音队列中的谈话。

SpeechSynthesis.getVoices():返回当前设备所有可用声音的 SpeechSynthesisVoice列表。

SpeechSynthesis.pause():暂停状态。

SpeechSynthesis.resume():非暂停状态:如果已经暂停了则继续。

SpeechSynthesis.speak():添加一个utterance到语音谈话队列;它将会在其他语音谈话播放完之后播放。
复制代码
复制代码
SpeechSynthesisUtterance.lang:设置话语的语言。 例如:“zh-cn”表示中文

SpeechSynthesisUtterance.pitch:设置说话的音调(音高)。范围从0(最小)到2(最大)。默认值为1

SpeechSynthesisUtterance.rate:设置说话的速度。默认值是1,范围是0.1到10,表示语速的倍数,例如2表示正常语速的两倍

SpeechSynthesisUtterance.text:设置在说话时将合成的文本内容。

SpeechSynthesisUtterance.voice:设置用于说话的声音。

SpeechSynthesisUtterance.volume:设置将在其中发言的音量。区间范围是0到1,默认是1
复制代码

上代码:

 

复制代码
const synth = window.speechSynthesis;
const msg = new SpeechSynthesisUtterance();
const speak = () => {
    window.speechSynthesis.cancel()
    msg.text = '危险,请小心驾驶'; //
    msg.lang = "zh-CN"; // 使用的语言:中文
    msg.volume = 20; // 声音音量:1
    msg.rate = 1; // 语速:1
    msg.pitch = 0; // 音高:1
    synth.speak(msg); // 播放
}
复制代码

 

posted @   幻影之舞  阅读(334)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
点击右上角即可分享
微信分享提示