web SpeechSynthesisUtterance 语音合成技术


介绍:

The SpeechSynthesisUtterance interface of the Web Speech API represents a speech request. It contains the content the speech service should read and information about how to read it (e.g. language, pitch and volume.).
也就是说SpeechSynthesisUtterance接口提供了语音服务,他可以阅读文本内容,以及配置语言,音量,音量。
让我们简单来实现以下。

示例

1
2
<input placeholder="Type your text"  type="text">
<button @click="speak"></button>
1
2
3
4
5
6
const speak = () => {
var text = document.querySelector('input');
var synth = window.speechSynthesis;
var utterThis = new SpeechSynthesisUtterance(text.value);
synth.speak(utterThis);
}

就这么简单,获取到文本内容,实例化SpeechSynthesisUtterance,通过speechSynthesisapi将其添加到语音队列即可。

兼容性

兼容大部分浏览器,不兼容Android浏览器

天马行空

这项技术是否可以为无障碍和辅助技术自定义报警语音聊天机器人

ok,今天的学习分享就到这了。

我的微信公众号: 梨的前端小屋


文章作者: 梨啊梨
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 梨啊梨 !
  目录