You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
1.2 KiB

<!DOCTYPE html>
<html>
<head>
<title>Speech Synthesis Example</title>
</head>
<body>
<h1>Speech Synthesis Example</h1>
<input type="text" id="text" value="请0的家属到手术室门口接病人">
<br>
<button onclick="speakText()">Speak</button>
<script>
const synth = window.speechSynthesis;
function speakText() {
const textInput = document.getElementById('text').value;
const utterance = new SpeechSynthesisUtterance(textInput);
// 固定播放速度和语音
utterance.rate = 1.0; // 播放速度
const voices = synth.getVoices();
const selectedVoice = voices.find(voice => voice.lang === 'zh-CN'); // 固定选择中文(普通话)的语音
if (selectedVoice) {
utterance.voice = selectedVoice;
}
// 添加 onend 事件处理器
utterance.onend = function(event) {
console.log('Speech has finished.');
};
window.speechSynthesis.speak(utterance);
}
// 等待语音列表加载完成
function checkVoices() {
if (synth.getVoices().length !== 0) {
clearInterval(voicesCheckInterval);
}
}
const voicesCheckInterval = setInterval(checkVoices, 100);
</script>
</body>
</html>