HTML5 语音合成
·
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>语音合成</title>
<style>
/* input{
width: 500px;
height: 200px;
} */
</style>
</head>
<body>
<div>
<h3>
<a href="https://blog.csdn.net/yb305/article/details/111219007" target="_blank">
语音合成使用 文字语音播报
</a>
</h3>
<h3>
<a href="https://www.jianshu.com/p/92dec635f6c5" target="_blank">
HTML5语音合成Speech Synthesis API简介
</a>
</h3>
<textarea rows="5" cols="100" id="input" placeholder="请输入内容"></textarea>
<div>
<p>
<label>语言:</label>
<select id="lange">
<option value="zh-cn" selected>中文</option>
<option value="en-US">英文</option>
</select>
</p>
<p>
<label>音量:</label><input type="range" min="0" max="1" step="0.1" id="volume" />
</p>
<p>
<label>音速:</label><input type="range" min="0" max="10" step="0.1" id="rate" />
</p>
<p>
<label>音色:</label><input type="range" min="0" max="2" step="0.1" id="pitch" />
</p>
</div>
<div>
<button type="button" id="submit">播报</button>
<button type="button" id="suspend">暂停</button>
<button type="button" id="recovery">恢复</button>
<button type="button" id="stop">停止</button>
</div>
</div>
<script>
// 1.获取input框输入的内容
function getValue() {
//定义全局对象
const obj = {
text: "",
lange: "zh-cn",
volume: 1,
rate: 1,
pitch: 1,
};
//点击“播报”按钮
const Dom = document.getElementById("submit");
Dom.onclick = function () {
const value = document.getElementById("input").value;
if (!value) return;
console.log("点击获取内容1", value);
obj.text = value;
speeck(obj);
};
//按下回车键按钮
window.onkeyup = function (e) {
// console.log("e",e);
const value = document.getElementById("input").value;
if (e.keyCode !== 13 || !value) return;
console.log("回车获取内容2", value);
obj.text = value;
speeck(obj);
};
//暂停播报
const suspend = document.getElementById("suspend");
suspend.onclick = function () {
window.plays.pause(); //暂停
};
//恢复播报
const recovery = document.getElementById("recovery");
recovery.onclick = function () {
window.plays.resume(); //恢复
};
//停止播报
const stop = document.getElementById("stop");
stop.onclick = function () {
window.plays.cancel(); //停止
};
//选择语言
const lange = document.getElementById("lange");
lange.onchange = function (v) {
console.log("选择语言", v);
console.log("选择语言-2", v.target.value);
obj.lange = v.target.value;
speeck(obj);
};
//选择音量
const volume = document.getElementById("volume");
volume.onchange = function (v) {
console.log("选择音量", v.target.value);
obj.volume = v.target.value;
speeck(obj);
};
//选择音速
const rate = document.getElementById("rate");
rate.onchange = function (v) {
console.log("选择音速", v.target.value);
obj.rate = v.target.value;
speeck(obj);
};
//选择音色
const pitch = document.getElementById("pitch");
pitch.onclick = function (v) {
console.log("选择音色", v.target.value);
obj.pitch = v.target.value;
speeck(obj);
};
}
//调用执行
getValue();
//2.语音播报
function speeck(data) {
console.log("播报时", data);
//SpeechSynthesisUtterance对象,主要用来构建语音合成实例
window.voice = new window.SpeechSynthesisUtterance();
// 对象合成方法
Object.assign(window.voice, data)
//speechSynthesis对象,主要作用是触发行为,例如读,停,还原
window.plays = window.speechSynthesis;
window.plays.speak(window.voice);
}
</script>
</body>
</html>
更多推荐



所有评论(0)