364 lines
11 KiB
HTML
364 lines
11 KiB
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>
|
|
<style>
|
|
body {
|
|
font-family: Arial, sans-serif;
|
|
max-width: 1200px;
|
|
margin: 20px auto;
|
|
padding: 20px;
|
|
}
|
|
.section {
|
|
margin: 20px 0;
|
|
padding: 15px;
|
|
border: 1px solid #ddd;
|
|
border-radius: 5px;
|
|
}
|
|
.section h2 {
|
|
margin-top: 0;
|
|
}
|
|
button {
|
|
padding: 10px 20px;
|
|
margin: 5px;
|
|
cursor: pointer;
|
|
background: #409eff;
|
|
color: white;
|
|
border: none;
|
|
border-radius: 4px;
|
|
}
|
|
button:hover {
|
|
background: #66b1ff;
|
|
}
|
|
button:disabled {
|
|
background: #ccc;
|
|
cursor: not-allowed;
|
|
}
|
|
#videoContainer {
|
|
display: flex;
|
|
gap: 10px;
|
|
margin: 10px 0;
|
|
}
|
|
video {
|
|
width: 320px;
|
|
height: 240px;
|
|
background: #000;
|
|
border: 2px solid #409eff;
|
|
}
|
|
#log {
|
|
background: #f5f5f5;
|
|
padding: 10px;
|
|
height: 300px;
|
|
overflow-y: auto;
|
|
font-family: monospace;
|
|
font-size: 12px;
|
|
white-space: pre-wrap;
|
|
}
|
|
.log-entry {
|
|
margin: 2px 0;
|
|
}
|
|
.log-info { color: #409eff; }
|
|
.log-success { color: #67c23a; }
|
|
.log-warning { color: #e6a23c; }
|
|
.log-error { color: #f56c6c; }
|
|
.status {
|
|
padding: 5px 10px;
|
|
border-radius: 3px;
|
|
display: inline-block;
|
|
margin: 5px 0;
|
|
}
|
|
.status.idle { background: #e4e7ed; }
|
|
.status.recording { background: #f56c6c; color: white; }
|
|
.status.stopped { background: #67c23a; color: white; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<h1>本地录制测试工具</h1>
|
|
|
|
<div class="section">
|
|
<h2>1. 浏览器支持检测</h2>
|
|
<div id="browserSupport"></div>
|
|
</div>
|
|
|
|
<div class="section">
|
|
<h2>2. 视频流测试</h2>
|
|
<button id="startCamera">启动摄像头</button>
|
|
<button id="stopCamera" disabled>停止摄像头</button>
|
|
<div id="videoContainer"></div>
|
|
</div>
|
|
|
|
<div class="section">
|
|
<h2>3. 录制测试</h2>
|
|
<div>
|
|
状态: <span class="status idle" id="recordStatus">未开始</span>
|
|
</div>
|
|
<button id="startRecord" disabled>开始录制</button>
|
|
<button id="stopRecord" disabled>停止录制</button>
|
|
<button id="playRecord" disabled>播放录制</button>
|
|
<button id="downloadRecord" disabled>下载录制</button>
|
|
<div id="recordInfo"></div>
|
|
</div>
|
|
|
|
<div class="section">
|
|
<h2>4. 日志</h2>
|
|
<button id="clearLog">清空日志</button>
|
|
<div id="log"></div>
|
|
</div>
|
|
|
|
<script>
|
|
const log = (message, type = 'info') => {
|
|
const logDiv = document.getElementById('log');
|
|
const entry = document.createElement('div');
|
|
entry.className = `log-entry log-${type}`;
|
|
entry.textContent = `[${new Date().toLocaleTimeString()}] ${message}`;
|
|
logDiv.appendChild(entry);
|
|
logDiv.scrollTop = logDiv.scrollHeight;
|
|
console.log(message);
|
|
};
|
|
|
|
// 1. 检测浏览器支持
|
|
const checkBrowserSupport = () => {
|
|
const support = document.getElementById('browserSupport');
|
|
const checks = [];
|
|
|
|
// MediaRecorder
|
|
if (typeof MediaRecorder !== 'undefined') {
|
|
checks.push('✅ MediaRecorder API 支持');
|
|
|
|
// MIME 类型
|
|
const mimes = [
|
|
'video/webm;codecs=vp9,opus',
|
|
'video/webm;codecs=vp8,opus',
|
|
'video/webm;codecs=vp9',
|
|
'video/webm'
|
|
];
|
|
mimes.forEach(mime => {
|
|
const supported = MediaRecorder.isTypeSupported(mime);
|
|
checks.push(`${supported ? '✅' : '❌'} ${mime}`);
|
|
if (supported) {
|
|
log(`支持 MIME: ${mime}`, 'success');
|
|
}
|
|
});
|
|
} else {
|
|
checks.push('❌ MediaRecorder API 不支持');
|
|
log('MediaRecorder API 不支持', 'error');
|
|
}
|
|
|
|
// getUserMedia
|
|
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
|
|
checks.push('✅ getUserMedia 支持');
|
|
} else {
|
|
checks.push('❌ getUserMedia 不支持');
|
|
log('getUserMedia 不支持', 'error');
|
|
}
|
|
|
|
// AudioContext
|
|
const AudioCtx = window.AudioContext || window.webkitAudioContext;
|
|
if (AudioCtx) {
|
|
checks.push('✅ AudioContext 支持');
|
|
} else {
|
|
checks.push('❌ AudioContext 不支持');
|
|
log('AudioContext 不支持', 'warning');
|
|
}
|
|
|
|
support.innerHTML = checks.join('<br>');
|
|
};
|
|
|
|
// 2. 视频流
|
|
let localStream = null;
|
|
const videoContainer = document.getElementById('videoContainer');
|
|
|
|
document.getElementById('startCamera').onclick = async () => {
|
|
try {
|
|
log('请求摄像头权限...', 'info');
|
|
localStream = await navigator.mediaDevices.getUserMedia({
|
|
video: true,
|
|
audio: true
|
|
});
|
|
|
|
const video = document.createElement('video');
|
|
video.srcObject = localStream;
|
|
video.autoplay = true;
|
|
video.muted = true;
|
|
videoContainer.appendChild(video);
|
|
|
|
log(`摄像头启动成功,轨道数: 视频=${localStream.getVideoTracks().length}, 音频=${localStream.getAudioTracks().length}`, 'success');
|
|
|
|
document.getElementById('startCamera').disabled = true;
|
|
document.getElementById('stopCamera').disabled = false;
|
|
document.getElementById('startRecord').disabled = false;
|
|
} catch (err) {
|
|
log(`摄像头启动失败: ${err.message}`, 'error');
|
|
}
|
|
};
|
|
|
|
document.getElementById('stopCamera').onclick = () => {
|
|
if (localStream) {
|
|
localStream.getTracks().forEach(track => track.stop());
|
|
videoContainer.innerHTML = '';
|
|
localStream = null;
|
|
log('摄像头已停止', 'info');
|
|
|
|
document.getElementById('startCamera').disabled = false;
|
|
document.getElementById('stopCamera').disabled = true;
|
|
document.getElementById('startRecord').disabled = true;
|
|
}
|
|
};
|
|
|
|
// 3. 录制
|
|
let mediaRecorder = null;
|
|
let recordedChunks = [];
|
|
let recordedBlob = null;
|
|
|
|
const updateRecordStatus = (status, text) => {
|
|
const statusEl = document.getElementById('recordStatus');
|
|
statusEl.className = `status ${status}`;
|
|
statusEl.textContent = text;
|
|
};
|
|
|
|
document.getElementById('startRecord').onclick = () => {
|
|
if (!localStream) {
|
|
log('请先启动摄像头', 'warning');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
recordedChunks = [];
|
|
recordedBlob = null;
|
|
|
|
// 尝试不同的 MIME 类型
|
|
const mimes = [
|
|
'video/webm;codecs=vp9,opus',
|
|
'video/webm;codecs=vp8,opus',
|
|
'video/webm;codecs=vp9',
|
|
'video/webm'
|
|
];
|
|
|
|
let selectedMime = '';
|
|
for (const mime of mimes) {
|
|
if (MediaRecorder.isTypeSupported(mime)) {
|
|
selectedMime = mime;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!selectedMime) {
|
|
log('没有找到支持的 MIME 类型', 'error');
|
|
return;
|
|
}
|
|
|
|
log(`使用 MIME 类型: ${selectedMime}`, 'info');
|
|
|
|
mediaRecorder = new MediaRecorder(localStream, { mimeType: selectedMime });
|
|
|
|
mediaRecorder.ondataavailable = (e) => {
|
|
if (e.data && e.data.size > 0) {
|
|
recordedChunks.push(e.data);
|
|
log(`收到数据块: ${e.data.size} bytes (总计: ${recordedChunks.length} 块)`, 'info');
|
|
}
|
|
};
|
|
|
|
mediaRecorder.onstart = () => {
|
|
log('录制已开始', 'success');
|
|
updateRecordStatus('recording', '录制中');
|
|
};
|
|
|
|
mediaRecorder.onstop = () => {
|
|
log(`录制已停止,共 ${recordedChunks.length} 个数据块`, 'success');
|
|
updateRecordStatus('stopped', '已停止');
|
|
|
|
if (recordedChunks.length > 0) {
|
|
recordedBlob = new Blob(recordedChunks, { type: selectedMime });
|
|
log(`生成 Blob: ${recordedBlob.size} bytes`, 'success');
|
|
|
|
document.getElementById('playRecord').disabled = false;
|
|
document.getElementById('downloadRecord').disabled = false;
|
|
|
|
const info = document.getElementById('recordInfo');
|
|
info.innerHTML = `
|
|
<p>录制完成!</p>
|
|
<p>大小: ${(recordedBlob.size / 1024 / 1024).toFixed(2)} MB</p>
|
|
<p>类型: ${recordedBlob.type}</p>
|
|
<p>数据块: ${recordedChunks.length}</p>
|
|
`;
|
|
} else {
|
|
log('警告: 没有收到任何数据块!', 'error');
|
|
}
|
|
};
|
|
|
|
mediaRecorder.onerror = (e) => {
|
|
log(`录制错误: ${e.error}`, 'error');
|
|
};
|
|
|
|
mediaRecorder.start(250); // 每 250ms 一个数据块
|
|
log('MediaRecorder.start(250) 调用成功', 'info');
|
|
|
|
document.getElementById('startRecord').disabled = true;
|
|
document.getElementById('stopRecord').disabled = false;
|
|
} catch (err) {
|
|
log(`录制启动失败: ${err.message}`, 'error');
|
|
}
|
|
};
|
|
|
|
document.getElementById('stopRecord').onclick = () => {
|
|
if (mediaRecorder && mediaRecorder.state !== 'inactive') {
|
|
log('请求停止录制...', 'info');
|
|
|
|
// 模拟实际代码的停止流程
|
|
if (mediaRecorder.requestData) {
|
|
mediaRecorder.requestData();
|
|
log('调用 requestData()', 'info');
|
|
}
|
|
|
|
setTimeout(() => {
|
|
log(`延迟后数据块数: ${recordedChunks.length}`, 'info');
|
|
mediaRecorder.stop();
|
|
}, 300);
|
|
|
|
document.getElementById('startRecord').disabled = false;
|
|
document.getElementById('stopRecord').disabled = true;
|
|
}
|
|
};
|
|
|
|
document.getElementById('playRecord').onclick = () => {
|
|
if (recordedBlob) {
|
|
const url = URL.createObjectURL(recordedBlob);
|
|
const video = document.createElement('video');
|
|
video.src = url;
|
|
video.controls = true;
|
|
video.style.width = '640px';
|
|
|
|
const info = document.getElementById('recordInfo');
|
|
info.innerHTML = '';
|
|
info.appendChild(video);
|
|
|
|
log('播放录制视频', 'info');
|
|
}
|
|
};
|
|
|
|
document.getElementById('downloadRecord').onclick = () => {
|
|
if (recordedBlob) {
|
|
const url = URL.createObjectURL(recordedBlob);
|
|
const a = document.createElement('a');
|
|
a.href = url;
|
|
a.download = `recording-${Date.now()}.webm`;
|
|
a.click();
|
|
URL.revokeObjectURL(url);
|
|
|
|
log('下载录制文件', 'success');
|
|
}
|
|
};
|
|
|
|
document.getElementById('clearLog').onclick = () => {
|
|
document.getElementById('log').innerHTML = '';
|
|
};
|
|
|
|
// 初始化
|
|
checkBrowserSupport();
|
|
log('测试工具已加载', 'success');
|
|
</script>
|
|
</body>
|
|
</html>
|