test_recording.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>录音功能测试</title>
  7. <style>
  8. body {
  9. font-family: Arial, sans-serif;
  10. max-width: 800px;
  11. margin: 0 auto;
  12. padding: 20px;
  13. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  14. color: white;
  15. min-height: 100vh;
  16. }
  17. .test-container {
  18. background: rgba(255, 255, 255, 0.1);
  19. padding: 20px;
  20. border-radius: 10px;
  21. margin: 20px 0;
  22. backdrop-filter: blur(10px);
  23. }
  24. .record-btn {
  25. background: linear-gradient(45deg, #4facfe, #00f2fe);
  26. border: none;
  27. color: white;
  28. padding: 20px 40px;
  29. border-radius: 50px;
  30. font-size: 18px;
  31. cursor: pointer;
  32. margin: 10px;
  33. transition: all 0.3s ease;
  34. }
  35. .record-btn:hover {
  36. transform: scale(1.05);
  37. }
  38. .record-btn.recording {
  39. background: linear-gradient(45deg, #ff6b6b, #ee5a52);
  40. animation: pulse 1s infinite;
  41. }
  42. @keyframes pulse {
  43. 0% { box-shadow: 0 0 20px rgba(255, 107, 107, 0.6); }
  44. 50% { box-shadow: 0 0 40px rgba(255, 107, 107, 0.8); }
  45. 100% { box-shadow: 0 0 20px rgba(255, 107, 107, 0.6); }
  46. }
  47. .log {
  48. background: rgba(0, 0, 0, 0.3);
  49. padding: 10px;
  50. border-radius: 5px;
  51. margin: 10px 0;
  52. font-family: monospace;
  53. max-height: 200px;
  54. overflow-y: auto;
  55. }
  56. .status {
  57. font-weight: bold;
  58. margin: 10px 0;
  59. }
  60. .error {
  61. color: #ff6b6b;
  62. }
  63. .success {
  64. color: #51cf66;
  65. }
  66. .info {
  67. color: #74c0fc;
  68. }
  69. </style>
  70. </head>
  71. <body>
  72. <h1>🎤 录音功能测试工具</h1>
  73. <div class="test-container">
  74. <h2>浏览器兼容性检查</h2>
  75. <div id="compatibility-results"></div>
  76. </div>
  77. <div class="test-container">
  78. <h2>录音测试</h2>
  79. <p>请先点击下面的按钮测试录音功能:</p>
  80. <button id="record-btn" class="record-btn">🎤 点击开始录音</button>
  81. <button id="long-press-btn" class="record-btn">👆 长按录音 (模拟原始功能)</button>
  82. <div class="status" id="status">等待开始...</div>
  83. <div id="recording-time" style="display: none;"></div>
  84. <h3>音频播放测试</h3>
  85. <button id="play-btn" class="record-btn" disabled>🔊 播放录音</button>
  86. <audio id="audio-player" controls style="width: 100%; margin: 10px 0; display: none;"></audio>
  87. </div>
  88. <div class="test-container">
  89. <h2>调试日志</h2>
  90. <div id="log" class="log"></div>
  91. <button onclick="clearLog()" class="record-btn">清空日志</button>
  92. </div>
  93. <script>
  94. let mediaRecorder = null;
  95. let isRecording = false;
  96. let audioChunks = [];
  97. let stream = null;
  98. let startTime = null;
  99. let timerInterval = null;
  100. // 日志功能
  101. function log(message, type = 'info') {
  102. const timestamp = new Date().toLocaleTimeString();
  103. const logDiv = document.getElementById('log');
  104. const logEntry = document.createElement('div');
  105. logEntry.className = type;
  106. logEntry.textContent = `${timestamp}: ${message}`;
  107. logDiv.appendChild(logEntry);
  108. logDiv.scrollTop = logDiv.scrollHeight;
  109. console.log(`[RecordingTest] ${message}`);
  110. }
  111. function clearLog() {
  112. document.getElementById('log').innerHTML = '';
  113. }
  114. function updateStatus(message, type = 'info') {
  115. const statusDiv = document.getElementById('status');
  116. statusDiv.textContent = message;
  117. statusDiv.className = `status ${type}`;
  118. }
  119. // 浏览器兼容性检查
  120. function checkCompatibility() {
  121. const features = {
  122. 'MediaRecorder': 'MediaRecorder' in window,
  123. 'getUserMedia': !!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia),
  124. 'AudioContext': 'AudioContext' in window || 'webkitAudioContext' in window,
  125. 'WebSocket': 'WebSocket' in window
  126. };
  127. const resultsDiv = document.getElementById('compatibility-results');
  128. let html = '<ul>';
  129. let allSupported = true;
  130. for (const [feature, supported] of Object.entries(features)) {
  131. const status = supported ? '✅ 支持' : '❌ 不支持';
  132. html += `<li>${feature}: ${status}</li>`;
  133. if (!supported) allSupported = false;
  134. log(`${feature}: ${supported ? '支持' : '不支持'}`, supported ? 'success' : 'error');
  135. }
  136. html += '</ul>';
  137. html += `<p><strong>整体兼容性: ${allSupported ? '✅ 良好' : '❌ 有问题'}</strong></p>`;
  138. resultsDiv.innerHTML = html;
  139. return allSupported;
  140. }
  141. // 开始录音
  142. async function startRecording() {
  143. try {
  144. log('请求麦克风权限...');
  145. updateStatus('请求麦克风权限...', 'info');
  146. stream = await navigator.mediaDevices.getUserMedia({
  147. audio: {
  148. sampleRate: 24000,
  149. channelCount: 1,
  150. echoCancellation: true,
  151. noiseSuppression: true
  152. }
  153. });
  154. log('麦克风权限获取成功');
  155. mediaRecorder = new MediaRecorder(stream, {
  156. mimeType: 'audio/webm;codecs=opus'
  157. });
  158. audioChunks = [];
  159. mediaRecorder.ondataavailable = (event) => {
  160. if (event.data.size > 0) {
  161. audioChunks.push(event.data);
  162. log(`录音数据块: ${event.data.size} bytes`);
  163. }
  164. };
  165. mediaRecorder.onstop = () => {
  166. log('录音停止,处理音频数据...');
  167. const audioBlob = new Blob(audioChunks, { type: 'audio/webm;codecs=opus' });
  168. log(`录音完成,总大小: ${audioBlob.size} bytes`);
  169. // 创建音频URL用于播放
  170. const audioUrl = URL.createObjectURL(audioBlob);
  171. const audioPlayer = document.getElementById('audio-player');
  172. audioPlayer.src = audioUrl;
  173. audioPlayer.style.display = 'block';
  174. const playBtn = document.getElementById('play-btn');
  175. playBtn.disabled = false;
  176. playBtn.onclick = () => audioPlayer.play();
  177. updateStatus('录音完成!可以点击播放按钮试听', 'success');
  178. };
  179. mediaRecorder.onerror = (event) => {
  180. log(`录音错误: ${event.error}`, 'error');
  181. updateStatus('录音发生错误', 'error');
  182. };
  183. // 开始录音
  184. mediaRecorder.start(100); // 每100ms一个数据块
  185. isRecording = true;
  186. startTime = Date.now();
  187. // 开始计时
  188. timerInterval = setInterval(updateTimer, 100);
  189. log('录音已开始');
  190. updateStatus('正在录音...', 'success');
  191. // 更新按钮状态
  192. updateRecordButton();
  193. } catch (error) {
  194. log(`录音启动失败: ${error.message}`, 'error');
  195. updateStatus(`录音失败: ${error.message}`, 'error');
  196. if (error.name === 'NotAllowedError') {
  197. updateStatus('麦克风权限被拒绝', 'error');
  198. } else if (error.name === 'NotFoundError') {
  199. updateStatus('未找到麦克风设备', 'error');
  200. }
  201. }
  202. }
  203. // 停止录音
  204. function stopRecording() {
  205. if (mediaRecorder && isRecording) {
  206. mediaRecorder.stop();
  207. isRecording = false;
  208. // 停止计时
  209. if (timerInterval) {
  210. clearInterval(timerInterval);
  211. timerInterval = null;
  212. }
  213. // 停止音频流
  214. if (stream) {
  215. stream.getTracks().forEach(track => track.stop());
  216. stream = null;
  217. }
  218. log('录音已停止');
  219. updateRecordButton();
  220. document.getElementById('recording-time').style.display = 'none';
  221. }
  222. }
  223. // 更新计时器
  224. function updateTimer() {
  225. if (startTime) {
  226. const elapsed = (Date.now() - startTime) / 1000;
  227. const minutes = Math.floor(elapsed / 60);
  228. const seconds = Math.floor(elapsed % 60);
  229. const timeStr = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
  230. const timerDiv = document.getElementById('recording-time');
  231. timerDiv.textContent = `录音时间: ${timeStr}`;
  232. timerDiv.style.display = 'block';
  233. }
  234. }
  235. // 更新录音按钮状态
  236. function updateRecordButton() {
  237. const recordBtn = document.getElementById('record-btn');
  238. const longPressBtn = document.getElementById('long-press-btn');
  239. if (isRecording) {
  240. recordBtn.textContent = '🛑 停止录音';
  241. recordBtn.classList.add('recording');
  242. longPressBtn.disabled = true;
  243. } else {
  244. recordBtn.textContent = '🎤 点击开始录音';
  245. recordBtn.classList.remove('recording');
  246. longPressBtn.disabled = false;
  247. }
  248. }
  249. // 长按录音功能
  250. let longPressTimer = null;
  251. let isLongPressing = false;
  252. function startLongPress() {
  253. isLongPressing = true;
  254. longPressTimer = setTimeout(() => {
  255. if (isLongPressing) {
  256. log('长按录音开始');
  257. startRecording();
  258. }
  259. }, 200); // 200ms后开始录音
  260. }
  261. function endLongPress() {
  262. isLongPressing = false;
  263. if (longPressTimer) {
  264. clearTimeout(longPressTimer);
  265. longPressTimer = null;
  266. }
  267. if (isRecording) {
  268. stopRecording();
  269. }
  270. }
  271. // 初始化
  272. window.onload = () => {
  273. log('页面加载完成,开始兼容性检查');
  274. checkCompatibility();
  275. // 绑定事件
  276. const recordBtn = document.getElementById('record-btn');
  277. recordBtn.onclick = () => {
  278. if (isRecording) {
  279. stopRecording();
  280. } else {
  281. startRecording();
  282. }
  283. };
  284. // 长按录音事件
  285. const longPressBtn = document.getElementById('long-press-btn');
  286. // 鼠标事件
  287. longPressBtn.onmousedown = (e) => {
  288. e.preventDefault();
  289. log('鼠标按下 - 开始长按');
  290. startLongPress();
  291. };
  292. longPressBtn.onmouseup = () => {
  293. log('鼠标松开 - 结束长按');
  294. endLongPress();
  295. };
  296. longPressBtn.onmouseleave = () => {
  297. log('鼠标离开 - 结束长按');
  298. endLongPress();
  299. };
  300. // 触摸事件
  301. longPressBtn.ontouchstart = (e) => {
  302. e.preventDefault();
  303. log('触摸开始 - 开始长按');
  304. startLongPress();
  305. };
  306. longPressBtn.ontouchend = (e) => {
  307. e.preventDefault();
  308. log('触摸结束 - 结束长按');
  309. endLongPress();
  310. };
  311. log('事件绑定完成,可以开始测试');
  312. };
  313. </script>
  314. </body>
  315. </html>