<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>语音合成</title>
<style>
body {
max-width: 500px;
margin: 20px auto;
padding: 20px;
font-family: Arial, sans-serif;
}
textarea {
width: 100%;
height: 100px;
margin: 10px 0;
padding: 10px;
}
button {
padding: 8px 16px;
margin-right: 10px;
background:
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background:
}
</style>
</head>
<body>
<h2>语音合成</h2>
<textarea id="text" placeholder="请输入要转换成语音的文字...">这是一个测试语音。</textarea>
<div>
<button onclick="speak()">播放</button>
<button onclick="stop()">停止</button>
</div>
<script>
const synth = window.speechSynthesis;
function speak() {
if (synth.speaking) {
synth.cancel();
}
const text = document.getElementById('text').value;
const utterance = new SpeechSynthesisUtterance(text);
utterance.lang = 'zh-CN'; // 设置为中文
synth.speak(utterance);
}
function stop() {
synth.cancel();
}
</script>
</body>
</html>