| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- <!DOCTYPE html>
- <html lang="zh">
- <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: 800px;
- margin: 0 auto;
- padding: 20px;
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
- color: white;
- min-height: 100vh;
- }
- .test-container {
- background: rgba(255, 255, 255, 0.1);
- padding: 20px;
- border-radius: 10px;
- margin: 20px 0;
- backdrop-filter: blur(10px);
- }
- .record-btn {
- background: linear-gradient(45deg, #4facfe, #00f2fe);
- border: none;
- color: white;
- padding: 20px 40px;
- border-radius: 50px;
- font-size: 18px;
- cursor: pointer;
- margin: 10px;
- transition: all 0.3s ease;
- }
- .record-btn:hover {
- transform: scale(1.05);
- }
- .record-btn.recording {
- background: linear-gradient(45deg, #ff6b6b, #ee5a52);
- animation: pulse 1s infinite;
- }
- @keyframes pulse {
- 0% { box-shadow: 0 0 20px rgba(255, 107, 107, 0.6); }
- 50% { box-shadow: 0 0 40px rgba(255, 107, 107, 0.8); }
- 100% { box-shadow: 0 0 20px rgba(255, 107, 107, 0.6); }
- }
- .log {
- background: rgba(0, 0, 0, 0.3);
- padding: 10px;
- border-radius: 5px;
- margin: 10px 0;
- font-family: monospace;
- max-height: 200px;
- overflow-y: auto;
- }
- .status {
- font-weight: bold;
- margin: 10px 0;
- }
- .error {
- color: #ff6b6b;
- }
- .success {
- color: #51cf66;
- }
- .info {
- color: #74c0fc;
- }
- </style>
- </head>
- <body>
- <h1>🎤 录音功能测试工具</h1>
-
- <div class="test-container">
- <h2>浏览器兼容性检查</h2>
- <div id="compatibility-results"></div>
- </div>
- <div class="test-container">
- <h2>录音测试</h2>
- <p>请先点击下面的按钮测试录音功能:</p>
-
- <button id="record-btn" class="record-btn">🎤 点击开始录音</button>
- <button id="long-press-btn" class="record-btn">👆 长按录音 (模拟原始功能)</button>
-
- <div class="status" id="status">等待开始...</div>
- <div id="recording-time" style="display: none;"></div>
-
- <h3>音频播放测试</h3>
- <button id="play-btn" class="record-btn" disabled>🔊 播放录音</button>
- <audio id="audio-player" controls style="width: 100%; margin: 10px 0; display: none;"></audio>
- </div>
- <div class="test-container">
- <h2>调试日志</h2>
- <div id="log" class="log"></div>
- <button onclick="clearLog()" class="record-btn">清空日志</button>
- </div>
- <script>
- let mediaRecorder = null;
- let isRecording = false;
- let audioChunks = [];
- let stream = null;
- let startTime = null;
- let timerInterval = null;
- // 日志功能
- function log(message, type = 'info') {
- const timestamp = new Date().toLocaleTimeString();
- const logDiv = document.getElementById('log');
- const logEntry = document.createElement('div');
- logEntry.className = type;
- logEntry.textContent = `${timestamp}: ${message}`;
- logDiv.appendChild(logEntry);
- logDiv.scrollTop = logDiv.scrollHeight;
- console.log(`[RecordingTest] ${message}`);
- }
- function clearLog() {
- document.getElementById('log').innerHTML = '';
- }
- function updateStatus(message, type = 'info') {
- const statusDiv = document.getElementById('status');
- statusDiv.textContent = message;
- statusDiv.className = `status ${type}`;
- }
- // 浏览器兼容性检查
- function checkCompatibility() {
- const features = {
- 'MediaRecorder': 'MediaRecorder' in window,
- 'getUserMedia': !!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia),
- 'AudioContext': 'AudioContext' in window || 'webkitAudioContext' in window,
- 'WebSocket': 'WebSocket' in window
- };
- const resultsDiv = document.getElementById('compatibility-results');
- let html = '<ul>';
- let allSupported = true;
- for (const [feature, supported] of Object.entries(features)) {
- const status = supported ? '✅ 支持' : '❌ 不支持';
- html += `<li>${feature}: ${status}</li>`;
- if (!supported) allSupported = false;
- log(`${feature}: ${supported ? '支持' : '不支持'}`, supported ? 'success' : 'error');
- }
- html += '</ul>';
- html += `<p><strong>整体兼容性: ${allSupported ? '✅ 良好' : '❌ 有问题'}</strong></p>`;
- resultsDiv.innerHTML = html;
- return allSupported;
- }
- // 开始录音
- async function startRecording() {
- try {
- log('请求麦克风权限...');
- updateStatus('请求麦克风权限...', 'info');
- stream = await navigator.mediaDevices.getUserMedia({
- audio: {
- sampleRate: 24000,
- channelCount: 1,
- echoCancellation: true,
- noiseSuppression: true
- }
- });
- log('麦克风权限获取成功');
-
- mediaRecorder = new MediaRecorder(stream, {
- mimeType: 'audio/webm;codecs=opus'
- });
- audioChunks = [];
- mediaRecorder.ondataavailable = (event) => {
- if (event.data.size > 0) {
- audioChunks.push(event.data);
- log(`录音数据块: ${event.data.size} bytes`);
- }
- };
- mediaRecorder.onstop = () => {
- log('录音停止,处理音频数据...');
- const audioBlob = new Blob(audioChunks, { type: 'audio/webm;codecs=opus' });
- log(`录音完成,总大小: ${audioBlob.size} bytes`);
-
- // 创建音频URL用于播放
- const audioUrl = URL.createObjectURL(audioBlob);
- const audioPlayer = document.getElementById('audio-player');
- audioPlayer.src = audioUrl;
- audioPlayer.style.display = 'block';
-
- const playBtn = document.getElementById('play-btn');
- playBtn.disabled = false;
- playBtn.onclick = () => audioPlayer.play();
-
- updateStatus('录音完成!可以点击播放按钮试听', 'success');
- };
- mediaRecorder.onerror = (event) => {
- log(`录音错误: ${event.error}`, 'error');
- updateStatus('录音发生错误', 'error');
- };
- // 开始录音
- mediaRecorder.start(100); // 每100ms一个数据块
- isRecording = true;
- startTime = Date.now();
-
- // 开始计时
- timerInterval = setInterval(updateTimer, 100);
-
- log('录音已开始');
- updateStatus('正在录音...', 'success');
-
- // 更新按钮状态
- updateRecordButton();
- } catch (error) {
- log(`录音启动失败: ${error.message}`, 'error');
- updateStatus(`录音失败: ${error.message}`, 'error');
-
- if (error.name === 'NotAllowedError') {
- updateStatus('麦克风权限被拒绝', 'error');
- } else if (error.name === 'NotFoundError') {
- updateStatus('未找到麦克风设备', 'error');
- }
- }
- }
- // 停止录音
- function stopRecording() {
- if (mediaRecorder && isRecording) {
- mediaRecorder.stop();
- isRecording = false;
-
- // 停止计时
- if (timerInterval) {
- clearInterval(timerInterval);
- timerInterval = null;
- }
-
- // 停止音频流
- if (stream) {
- stream.getTracks().forEach(track => track.stop());
- stream = null;
- }
-
- log('录音已停止');
- updateRecordButton();
- document.getElementById('recording-time').style.display = 'none';
- }
- }
- // 更新计时器
- function updateTimer() {
- if (startTime) {
- const elapsed = (Date.now() - startTime) / 1000;
- const minutes = Math.floor(elapsed / 60);
- const seconds = Math.floor(elapsed % 60);
- const timeStr = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
-
- const timerDiv = document.getElementById('recording-time');
- timerDiv.textContent = `录音时间: ${timeStr}`;
- timerDiv.style.display = 'block';
- }
- }
- // 更新录音按钮状态
- function updateRecordButton() {
- const recordBtn = document.getElementById('record-btn');
- const longPressBtn = document.getElementById('long-press-btn');
-
- if (isRecording) {
- recordBtn.textContent = '🛑 停止录音';
- recordBtn.classList.add('recording');
- longPressBtn.disabled = true;
- } else {
- recordBtn.textContent = '🎤 点击开始录音';
- recordBtn.classList.remove('recording');
- longPressBtn.disabled = false;
- }
- }
- // 长按录音功能
- let longPressTimer = null;
- let isLongPressing = false;
- function startLongPress() {
- isLongPressing = true;
- longPressTimer = setTimeout(() => {
- if (isLongPressing) {
- log('长按录音开始');
- startRecording();
- }
- }, 200); // 200ms后开始录音
- }
- function endLongPress() {
- isLongPressing = false;
- if (longPressTimer) {
- clearTimeout(longPressTimer);
- longPressTimer = null;
- }
- if (isRecording) {
- stopRecording();
- }
- }
- // 初始化
- window.onload = () => {
- log('页面加载完成,开始兼容性检查');
- checkCompatibility();
- // 绑定事件
- const recordBtn = document.getElementById('record-btn');
- recordBtn.onclick = () => {
- if (isRecording) {
- stopRecording();
- } else {
- startRecording();
- }
- };
- // 长按录音事件
- const longPressBtn = document.getElementById('long-press-btn');
-
- // 鼠标事件
- longPressBtn.onmousedown = (e) => {
- e.preventDefault();
- log('鼠标按下 - 开始长按');
- startLongPress();
- };
-
- longPressBtn.onmouseup = () => {
- log('鼠标松开 - 结束长按');
- endLongPress();
- };
-
- longPressBtn.onmouseleave = () => {
- log('鼠标离开 - 结束长按');
- endLongPress();
- };
- // 触摸事件
- longPressBtn.ontouchstart = (e) => {
- e.preventDefault();
- log('触摸开始 - 开始长按');
- startLongPress();
- };
-
- longPressBtn.ontouchend = (e) => {
- e.preventDefault();
- log('触摸结束 - 结束长按');
- endLongPress();
- };
- log('事件绑定完成,可以开始测试');
- };
- </script>
- </body>
- </html>
|