| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <!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: 600px;
- margin: 50px auto;
- padding: 20px;
- text-align: center;
- background-color: #f5f5f5;
- }
- .container {
- background: white;
- padding: 20px;
- border-radius: 10px;
- box-shadow: 0 2px 10px rgba(0,0,0,0.1);
- }
- h1 {
- color: #333;
- }
- .volume-display {
- margin: 20px 0;
- font-size: 24px;
- font-weight: bold;
- color: #2c3e50;
- }
- .volume-bar {
- width: 100%;
- height: 30px;
- background: #ecf0f1;
- border-radius: 15px;
- margin: 20px 0;
- overflow: hidden;
- }
- .volume-level {
- height: 100%;
- background: linear-gradient(90deg, #2ecc71, #f1c40f, #e74c3c);
- width: 0%;
- transition: width 0.1s;
- }
- button {
- background: #3498db;
- color: white;
- border: none;
- padding: 10px 20px;
- font-size: 16px;
- border-radius: 5px;
- cursor: pointer;
- margin: 5px;
- }
- button:hover {
- background: #2980b9;
- }
- button:disabled {
- background: #bdc3c7;
- cursor: not-allowed;
- }
- .status {
- margin-top: 15px;
- font-size: 14px;
- color: #7f8c8d;
- }
- </style>
- </head>
- <body>
- <div class="container">
- <h1>音频音量监测</h1>
- <audio id="audioF" src="https://cunchu.site/work/storage/soundSynthesis/1749269905file.wav"></audio>
-
- <div class="volume-display" id="volumeValue">-∞ dB</div>
- <div class="volume-bar">
- <div class="volume-level" id="volumeLevel"></div>
- </div>
-
- <button id="playBtn">播放</button>
- <button id="pauseBtn" disabled>暂停</button>
-
- <div class="status" id="status">准备就绪</div>
- </div>
- <script>
- // 获取DOM元素
- const audio = document.getElementById("audioF");
- const playBtn = document.getElementById("playBtn");
- const pauseBtn = document.getElementById("pauseBtn");
- const volumeValue = document.getElementById("volumeValue");
- const volumeLevel = document.getElementById("volumeLevel");
- const status = document.getElementById("status");
-
- // 音频分析变量
- let audioContext, analyser, dataArray;
-
- // 初始化音频分析
- function initAudioAnalysis() {
- audioContext = new (window.AudioContext || window.webkitAudioContext)();
- analyser = audioContext.createAnalyser();
- analyser.fftSize = 256;
-
- const source = audioContext.createMediaElementSource(audio);
- source.connect(analyser);
- analyser.connect(audioContext.destination);
-
- dataArray = new Uint8Array(analyser.frequencyBinCount);
- }
-
- // 分析音频音量
- function analyzeVolume() {
- if (!analyser) return;
-
- analyser.getByteFrequencyData(dataArray);
-
- // 计算平均音量
- let sum = 0;
- for (let i = 0; i < dataArray.length; i++) {
- sum += dataArray[i];
- }
- const average = sum / dataArray.length;
-
- // 转换为分贝 (0-255 映射到 -100dB 到 0dB)
- const db = average > 0 ? 20 * Math.log10(average / 255) + 100 - 100 : -100;
-
- // 更新音量显示
- updateVolumeDisplay(db);
-
- // 继续分析
- if (!audio.paused) {
- requestAnimationFrame(analyzeVolume);
- }
- }
-
- // 更新音量显示
- function updateVolumeDisplay(db) {
- // 限制db范围在-100到0之间
- const clampedDb = Math.max(-100, Math.min(0, db));
- const displayDb = clampedDb.toFixed(1);
-
- volumeValue.textContent = `${displayDb} dB`;
-
- // 更新音量条 (将-100到0映射到0%到100%)
- const volumePercent = ((clampedDb + 100) / 100) * 100;
- volumeLevel.style.width = `${volumePercent}%`;
-
- // 更新状态
- if (clampedDb > -50) {
- status.textContent = "检测到声音";
- status.style.color = "#27ae60";
- } else {
- status.textContent = "静音";
- status.style.color = "#e74c3c";
- }
- }
-
- // 播放按钮事件
- playBtn.addEventListener("click", function() {
- audio.play();
- playBtn.disabled = true;
- pauseBtn.disabled = false;
- status.textContent = "播放中...";
-
- // 初始化音频分析
- if (!audioContext) {
- initAudioAnalysis();
- }
-
- // 开始分析音量
- analyzeVolume();
- });
-
- // 暂停按钮事件
- pauseBtn.addEventListener("click", function() {
- audio.pause();
- playBtn.disabled = false;
- pauseBtn.disabled = true;
- status.textContent = "已暂停";
- });
-
- // 音频结束事件
- audio.addEventListener("ended", function() {
- playBtn.disabled = false;
- pauseBtn.disabled = true;
- status.textContent = "播放结束";
- });
- </script>
- </body>
- </html>
|