parent
3bbdb3301c
commit
81d7140c35
@ -1,46 +1,69 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>Speech Synthesis Example</title>
|
<title>Speech Synthesis Example</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Speech Synthesis Example</h1>
|
<h1>Speech Synthesis Example</h1>
|
||||||
<input type="text" id="text" value="请0的家属到手术室门口接病人">
|
<input type="text" id="text" value="请0的家属到手术室门口接病人" />
|
||||||
<br>
|
<br />
|
||||||
<button onclick="speakText()">Speak</button>
|
<label for="rate">Rate:</label>
|
||||||
|
<input type="number" id="rate" value="1" step="0.1" min="0.1" max="10" />
|
||||||
<script>
|
<br />
|
||||||
const synth = window.speechSynthesis;
|
<label for="voice">Voice:</label>
|
||||||
|
<select id="voiceSelect"></select>
|
||||||
function speakText() {
|
<br />
|
||||||
const textInput = document.getElementById('text').value;
|
<button onclick="speakText()">Speak</button>
|
||||||
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 事件处理器
|
<script>
|
||||||
utterance.onend = function(event) {
|
const synth = window.speechSynthesis;
|
||||||
console.log('Speech has finished.');
|
const voiceSelect = document.getElementById('voiceSelect');
|
||||||
};
|
|
||||||
|
|
||||||
window.speechSynthesis.speak(utterance);
|
function populateVoiceList() {
|
||||||
}
|
const voices = synth.getVoices();
|
||||||
|
console.log(voices);
|
||||||
|
if (voices.length === 0) {
|
||||||
|
synth.onvoiceschanged = populateVoiceList;
|
||||||
|
} else {
|
||||||
|
voiceSelect.innerHTML = ''; // 清空之前的选项
|
||||||
|
voices.forEach((voice, index) => {
|
||||||
|
const option = document.createElement('option');
|
||||||
|
option.textContent = `${voice.name} (${voice.lang})`;
|
||||||
|
console.log(`${voice.name} (${voice.lang})`);
|
||||||
|
option.value = index;
|
||||||
|
voiceSelect.appendChild(option);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 等待语音列表加载完成
|
populateVoiceList();
|
||||||
function checkVoices() {
|
if (typeof synth.onvoiceschanged !== 'undefined') {
|
||||||
if (synth.getVoices().length !== 0) {
|
synth.onvoiceschanged = populateVoiceList;
|
||||||
clearInterval(voicesCheckInterval);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
const voicesCheckInterval = setInterval(checkVoices, 100);
|
function speakText() {
|
||||||
</script>
|
window.speechSynthesis.cancel(); // 清除之前的语音合成
|
||||||
</body>
|
|
||||||
|
setTimeout(() => {
|
||||||
|
const textInput = document.getElementById('text').value;
|
||||||
|
const rate = parseFloat(document.getElementById('rate').value);
|
||||||
|
|
||||||
|
|
||||||
|
const utterance = new SpeechSynthesisUtterance([textInput, textInput, textInput].join(''));
|
||||||
|
|
||||||
|
const selectedVoiceIndex = voiceSelect.selectedOptions[0].value;
|
||||||
|
const voices = synth.getVoices();
|
||||||
|
utterance.voice = voices[selectedVoiceIndex];
|
||||||
|
utterance.rate = rate;
|
||||||
|
|
||||||
|
utterance.onend = function (event) {
|
||||||
|
console.log('Speech has finished.');
|
||||||
|
alert('Speech has finished.');
|
||||||
|
};
|
||||||
|
|
||||||
|
window.speechSynthesis.speak(utterance);
|
||||||
|
}, 100); // 延迟 100 毫秒
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
Loading…
Reference in new issue