The Speech Synthesis API is an awesome tool provided by modern browsers.

语音合成API

现代浏览器都支持

desc

上手

直接使用即可

// 直接使用
speechSynthesis.speak(new SpeechSynthesisUtterance('Hey'))

// 简单封装一下
function speak(str) {
    if (!str) return;
    str = str.replace(/<[^>]+>/g, "");
    var utter = new window.SpeechSynthesisUtterance(str);
    utter.lang = 'zh-cn';
    utter.volume = 1;
    utter.rate = 1.4;
    utter.pitch = 2;
    speechSynthesis.speak(utter)
}

参考:



blog comments powered by Disqus

Published

08 January 2022

Tags