test.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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: 600px;
  11. margin: 50px auto;
  12. padding: 20px;
  13. text-align: center;
  14. background-color: #f5f5f5;
  15. }
  16. .container {
  17. background: white;
  18. padding: 20px;
  19. border-radius: 10px;
  20. box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  21. }
  22. h1 {
  23. color: #333;
  24. }
  25. .volume-display {
  26. margin: 20px 0;
  27. font-size: 24px;
  28. font-weight: bold;
  29. color: #2c3e50;
  30. }
  31. .volume-bar {
  32. width: 100%;
  33. height: 30px;
  34. background: #ecf0f1;
  35. border-radius: 15px;
  36. margin: 20px 0;
  37. overflow: hidden;
  38. }
  39. .volume-level {
  40. height: 100%;
  41. background: linear-gradient(90deg, #2ecc71, #f1c40f, #e74c3c);
  42. width: 0%;
  43. transition: width 0.1s;
  44. }
  45. button {
  46. background: #3498db;
  47. color: white;
  48. border: none;
  49. padding: 10px 20px;
  50. font-size: 16px;
  51. border-radius: 5px;
  52. cursor: pointer;
  53. margin: 5px;
  54. }
  55. button:hover {
  56. background: #2980b9;
  57. }
  58. button:disabled {
  59. background: #bdc3c7;
  60. cursor: not-allowed;
  61. }
  62. .status {
  63. margin-top: 15px;
  64. font-size: 14px;
  65. color: #7f8c8d;
  66. }
  67. </style>
  68. </head>
  69. <body>
  70. <div class="container">
  71. <h1>音频音量监测</h1>
  72. <audio id="audioF" src="https://cunchu.site/work/storage/soundSynthesis/1749269905file.wav"></audio>
  73. <div class="volume-display" id="volumeValue">-∞ dB</div>
  74. <div class="volume-bar">
  75. <div class="volume-level" id="volumeLevel"></div>
  76. </div>
  77. <button id="playBtn">播放</button>
  78. <button id="pauseBtn" disabled>暂停</button>
  79. <div class="status" id="status">准备就绪</div>
  80. </div>
  81. <script>
  82. // 获取DOM元素
  83. const audio = document.getElementById("audioF");
  84. const playBtn = document.getElementById("playBtn");
  85. const pauseBtn = document.getElementById("pauseBtn");
  86. const volumeValue = document.getElementById("volumeValue");
  87. const volumeLevel = document.getElementById("volumeLevel");
  88. const status = document.getElementById("status");
  89. // 音频分析变量
  90. let audioContext, analyser, dataArray;
  91. // 初始化音频分析
  92. function initAudioAnalysis() {
  93. audioContext = new (window.AudioContext || window.webkitAudioContext)();
  94. analyser = audioContext.createAnalyser();
  95. analyser.fftSize = 256;
  96. const source = audioContext.createMediaElementSource(audio);
  97. source.connect(analyser);
  98. analyser.connect(audioContext.destination);
  99. dataArray = new Uint8Array(analyser.frequencyBinCount);
  100. }
  101. // 分析音频音量
  102. function analyzeVolume() {
  103. if (!analyser) return;
  104. analyser.getByteFrequencyData(dataArray);
  105. // 计算平均音量
  106. let sum = 0;
  107. for (let i = 0; i < dataArray.length; i++) {
  108. sum += dataArray[i];
  109. }
  110. const average = sum / dataArray.length;
  111. // 转换为分贝 (0-255 映射到 -100dB 到 0dB)
  112. const db = average > 0 ? 20 * Math.log10(average / 255) + 100 - 100 : -100;
  113. // 更新音量显示
  114. updateVolumeDisplay(db);
  115. // 继续分析
  116. if (!audio.paused) {
  117. requestAnimationFrame(analyzeVolume);
  118. }
  119. }
  120. // 更新音量显示
  121. function updateVolumeDisplay(db) {
  122. // 限制db范围在-100到0之间
  123. const clampedDb = Math.max(-100, Math.min(0, db));
  124. const displayDb = clampedDb.toFixed(1);
  125. volumeValue.textContent = `${displayDb} dB`;
  126. // 更新音量条 (将-100到0映射到0%到100%)
  127. const volumePercent = ((clampedDb + 100) / 100) * 100;
  128. volumeLevel.style.width = `${volumePercent}%`;
  129. // 更新状态
  130. if (clampedDb > -50) {
  131. status.textContent = "检测到声音";
  132. status.style.color = "#27ae60";
  133. } else {
  134. status.textContent = "静音";
  135. status.style.color = "#e74c3c";
  136. }
  137. }
  138. // 播放按钮事件
  139. playBtn.addEventListener("click", function() {
  140. audio.play();
  141. playBtn.disabled = true;
  142. pauseBtn.disabled = false;
  143. status.textContent = "播放中...";
  144. // 初始化音频分析
  145. if (!audioContext) {
  146. initAudioAnalysis();
  147. }
  148. // 开始分析音量
  149. analyzeVolume();
  150. });
  151. // 暂停按钮事件
  152. pauseBtn.addEventListener("click", function() {
  153. audio.pause();
  154. playBtn.disabled = false;
  155. pauseBtn.disabled = true;
  156. status.textContent = "已暂停";
  157. });
  158. // 音频结束事件
  159. audio.addEventListener("ended", function() {
  160. playBtn.disabled = false;
  161. pauseBtn.disabled = true;
  162. status.textContent = "播放结束";
  163. });
  164. </script>
  165. </body>
  166. </html>