const questions = [
'请介绍你对机器学习基本原理的理解?',
'请谈谈你对人工智能未来发展的看法?',
'请分享一次你解决复杂问题的经历?'
];
let currentQuestion = 0;
let recordedVideos = [];
let mediaRecorder;
let recordedChunks = [];
const questionNumElem = document.querySelector('h2 strong');
const questionTextElem = document.querySelector('p strong');
const preview = document.getElementById('preview');
const startBtn = document.getElementById('startBtn');
const stopBtn = document.getElementById('stopBtn');
const feedback = document.getElementById('feedback');
const radarImg = document.getElementById('radarImg');
function updateQuestion() {
if (currentQuestion < questions.length) {
questionNumElem.textContent = currentQuestion + 1;
questionTextElem.textContent = questions[currentQuestion];
feedback.textContent = `请点击“开始录制”录制第${currentQuestion+1}题视频。`;
radarImg.style.display = 'none';
}
}
startBtn.onclick = async () => {
recordedChunks = [];
try {
let stream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true });
preview.srcObject = stream;
mediaRecorder = new MediaRecorder(stream);
mediaRecorder.ondataavailable = e => {
if (e.data.size > 0) recordedChunks.push(e.data);
};
mediaRecorder.onstop = () => {
preview.srcObject.getTracks().forEach(track => track.stop());
const blob = new Blob(recordedChunks, { type: 'video/webm' });
recordedVideos.push(blob);
currentQuestion++;
if (currentQuestion < questions.length) {
updateQuestion();
startBtn.disabled = false;
stopBtn.disabled = true;
feedback.textContent = `第${currentQuestion}题录制完成,请准备第${currentQuestion+1}题`;
} else {
startBtn.disabled = true;
stopBtn.disabled = true;
feedback.textContent = "三题录制完成,正在上传并分析...";
uploadAllVideos();
}
};
mediaRecorder.start();
startBtn.disabled = true;
stopBtn.disabled = false;
feedback.textContent = "录制中...";
} catch (err) {
feedback.textContent = "摄像头权限被拒绝或设备不可用: " + err;
}
};
stopBtn.onclick = () => {
mediaRecorder.stop();
};
function uploadAllVideos() {
const formData = new FormData();
recordedVideos.forEach((blob, idx) => {
formData.append('file' + (idx+1), blob, `interview${idx+1}.webm`);
});
fetch('/upload_multiple', { method: 'POST', body: formData })
.then(resp => resp.json())
.then(data => {
if (data.error) {
feedback.textContent = "上传或分析出错:" + data.error;
radarImg.style.display = "none";
} else {
feedback.textContent = data.feedback;
radarImg.src = "/static/radar.png?" + new Date().getTime();
radarImg.style.display = "block";
}
})
.catch(err => {
feedback.textContent = "上传失败: " + err;
radarImg.style.display = "none";
});
}
updateQuestion();