来源:so.csdn.net
发布时间:Nov 30, 2020, 1:26:10 AM
原地址:https://blog.csdn.net/glmushroom/article/details/110388136
js代码:
var wordSpeaker = {
_synth: null,
_msg: null,
// 0-2,声音的尖锐程度
pitch: 1,
//0-3,声音播放速率
rate: 1,
Speak: function (words) {
if (words == null)
{
return;
}
if (this._synth == null)
{
this._synth = window.speechSynthesis;
}
if (this._msg == null)
{
this._msg = new SpeechSynthesisUtterance();
}
this._msg.lang = 'zh-CN';
this._msg["rate"] = this.rate;
this._msg["pitch"] = this.pitch;
this._msg["text"] = words;
this._synth.speak(this._msg);
},
Stop: function ()
{
if (this._synth == null)
{
return;
}
this._synth.cancel();
}
}
调用时引用:
<script src="~/scripts/js/common/wordSpeaker.js"></script>
调用:
wordSpeaker.Speak(“要播放的文字”);
注意:谷歌浏览器不支持无用户激活的自动播放,需要在谷歌声音设置里将网站添加到允许列表里。