语音播报功能开发
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<title>Document</title>
<script type="text/javascript">
var speaker = new window.SpeechSynthesisUtterance();
var speakTimer,
stopTimer;
// 开始朗读
function speakText() {
var context = document.getElementById('ttsText');
clearTimeout(speakTimer);
window.speechSynthesis.cancel();
speakTimer = setTimeout(function () {
speaker.volume = 0.9
speaker.text = context.innerHTML;
window.speechSynthesis.speak(speaker);
}, 200);
}
// 停止朗读
function stopSpeak() {
clearTimeout(stopTimer);
clearTimeout(speakTimer);
stopTimer = setTimeout(function () {
window.speechSynthesis.cancel();
}, 20);
}
</script>
</head>
<body>
<p id="ttsText">
蟠龙会议室有订单信息,请注意查看
</p>
<div>
<input type="button" id="start_btn" onclick="speakText()" value="播放">
<input type="button" id="cancel_btn" onclick="stopSpeak()" value="取消">
</div>
</body>
</html>