html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>普通话学习助手</title>
<!-- 引入谷歌字体 -->
<link href="https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@400;700&display=swap" rel="stylesheet">
<style>
/* 重置样式 */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Noto Sans SC', Arial, sans-serif;
background: linear-gradient(135deg, #f0f4f8 0%, #d9e8ff 100%);
color: #333;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
.container {
background-color: rgba(255, 255, 255, 0.9);
backdrop-filter: blur(5px);
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
padding: 40px;
border-radius: 16px;
text-align: center;
max-width: 600px;
width: 90%;
animation: fadeIn 1s ease;
}
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(30px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
h1 {
color: #1e88e5;
font-size: 2.5em;
margin-bottom: 30px;
}
button {
background-color: #1e88e5;
color: white;
border: none;
padding: 15px 30px;
font-size: 1.1em;
border-radius: 50px;
cursor: pointer;
transition: all 0.3s ease;
position: relative;
overflow: hidden;
}
button::after {
content: '';
position: absolute;
left: -10%;
top: -50%;
width: 120%;
height: 200%;
background: rgba(255, 255, 255, 0.3);
transform: rotate(45deg);
opacity: 0;
transition: all 0.5s ease;
}
button:hover {
background-color: #1565c0;
}
button:hover::after {
top: -20%;
opacity: 1;
}
#result {
margin-top: 30px;
padding: 30px;
border-radius: 16px;
background-color: rgba(250, 250, 250, 0.9);
box-shadow: inset 0 0 15px rgba(0, 0, 0, 0.05);
text-align: left;
}
p {
margin: 15px 0;
font-size: 1.2em;
color: #333;
line-height: 1.6;
}
p strong {
color: #1e88e5;
}
audio {
margin-top: 20px;
width: 100%;
outline: none;
}
/* 响应式设计 */
@media (max-width: 480px) {
h1 {
font-size: 2em;
}
button {
padding: 12px 24px;
font-size: 1em;
}
p {
font-size: 1em;
}
}
</style>
</head>
<body>
<div class="container">
<h1>普通话学习助手</h1>
<button id="recordButton">开始录音</button>
<div id="result"></div>
</div>
<script>
const recordButton = document.getElementById('recordButton');
const resultDiv = document.getElementById('result');
let mediaRecorder;
let audioChunks = [];
recordButton.addEventListener('click', async () => {
if (recordButton.textContent === '开始录音') {
await startRecording();
recordButton.textContent = '停止录音';
} else {
stopRecording();
recordButton.textContent = '开始录音';
}
});
async function startRecording() {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
mediaRecorder = new MediaRecorder(stream, {
mimeType: 'audio/webm;codecs=opus',
audioBitsPerSecond: 16000
});
mediaRecorder.ondataavailable = event => {
audioChunks.push(event.data);
};
mediaRecorder.onstop = async () => {
const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
audioChunks = [];
const formData = new FormData();
formData.append('audio', audioBlob, 'recording.wav');
try {
const response = await fetch('http://127.0.0.1:5000/getJson', {
method: 'POST',
body: formData
});
const result = await response.json();
displayResult(result);
} catch (error) {
console.error('Error:', error);
alert('处理录音时出错,请重试。');
}
};
mediaRecorder.start();
}
function stopRecording() {
mediaRecorder.stop();
}
function displayResult(result) {
resultDiv.innerHTML = `
<p><strong>识别文本:</strong>${result.text}</p>
<p><strong>拼音:</strong>${result.pinyin}</p>
<p><strong>修正建议:</strong>${result.suggestions}</p>
<audio controls src="${result.speech_url}"></audio>
`;
}
</script>
</body>
</html>