JavaScript网页录音机中WebSocket代码插入位置及作用域问题咨询
问题
我是JavaScript新手,仅掌握基础语法,此前主要使用Python和少量VBA。为完成学校项目,需要搭建一个浏览器录音界面,将音频传输到运行语音转文字应用的服务器。我找到了MDN的网页录音机代码,但该代码缺少WebSocket部分,不清楚应将WebSocket代码插入何处。
目前将两段代码堆叠在一起后出现ws未定义的错误,这是变量作用域导致的——WebSocket变量被定义在DOMContentLoaded的回调函数内部,无法在录音逻辑的if语句块中访问。尝试将WebSocket代码移入if语句也未成功,希望了解如何调整代码解决问题。
原网页录音机代码:
// set up basic variables for app const record = document.querySelector('.record'); const stop = document.querySelector('.stop'); const soundClips = document.querySelector('.sound-clips'); const canvas = document.querySelector('.visualizer'); const mainSection = document.querySelector('.main-controls'); // disable stop button while not recording stop.disabled = true; // visualiser setup - create web audio api context and canvas let audioCtx; const canvasCtx = canvas.getContext("2d"); //main block for doing the audio recording if (navigator.mediaDevices.getUserMedia) { console.log('getUserMedia supported.'); const constraints = { audio: true }; let chunks = []; let onSuccess = function(stream) { const mediaRecorder = new MediaRecorder(stream); visualize(stream); record.onclick = function() { mediaRecorder.start(); console.log(mediaRecorder.state); console.log("recorder started"); record.style.background = "red"; stop.disabled = false; record.disabled = true; } stop.onclick = function() { mediaRecorder.stop(); console.log(mediaRecorder.state); console.log("recorder stopped"); record.style.background = ""; record.style.color = ""; // mediaRecorder.requestData(); stop.disabled = true; record.disabled = false; } mediaRecorder.onstop = function(e) { console.log("data available after MediaRecorder.stop() called."); const clipName = prompt('Enter a name for your sound clip?','My unnamed clip'); const clipContainer = document.createElement('article'); const clipLabel = document.createElement('p'); const audio = document.createElement('audio'); const deleteButton = document.createElement('button'); clipContainer.classList.add('clip'); audio.setAttribute('controls', ''); deleteButton.textContent = 'Delete'; deleteButton.className = 'delete'; if(clipName === null) { clipLabel.textContent = 'My unnamed clip'; } else { clipLabel.textContent = clipName; } clipContainer.appendChild(audio); clipContainer.appendChild(clipLabel); clipContainer.appendChild(deleteButton); soundClips.appendChild(clipContainer); audio.controls = true; const blob = new Blob(chunks, { 'type' : 'audio/ogg; codecs=opus' }); chunks = []; const audioURL = window.URL.createObjectURL(blob); audio.src = audioURL; console.log("recorder stopped"); deleteButton.onclick = function(e) { e.target.closest(".clip").remove(); } clipLabel.onclick = function() { const existingName = clipLabel.textContent; const newClipName = prompt('Enter a new name for your sound clip?'); if(newClipName === null) { clipLabel.textContent = existingName; } else { clipLabel.textContent = newClipName; } } } mediaRecorder.ondataavailable = function(e) { chunks.push(e.data); } } let onError = function(err) { console.log('The following error occured: ' + err); } navigator.mediaDevices.getUserMedia(constraints).then(onSuccess, onError); } else { console.log('getUserMedia not supported on your browser!'); }
原客户端WebSocket代码:
window.addEventListener("DOMContentLoaded", () => { // Open the WebSocket connection and register event handlers. console.log('DOMContentLoaded done'); const ws = new WebSocket("ws://localhost:8001/"); // temp moved to mediarecorder.onstop dataToBeSent = function (data) { ws.send(data); }; console.log('ws is defined'); })
解决方法
1. 调整WebSocket变量作用域
将ws变量提升到全局作用域(或者录音代码能访问到的外层作用域),避免因作用域隔离导致无法访问。
2. 确保WebSocket连接就绪后再发送数据
WebSocket连接是异步建立的,必须等待onopen事件触发后再发送音频数据,否则会出现发送失败的情况。
3. 在录音结束时发送音频Blob
在mediaRecorder.onstop事件中,生成音频Blob后直接通过WebSocket发送给服务器。
修改后的完整代码
// 全局作用域声明WebSocket变量,让录音逻辑能访问 let ws; let isWsReady = false; // set up basic variables for app const record = document.querySelector('.record'); const stop = document.querySelector('.stop'); const soundClips = document.querySelector('.sound-clips'); const canvas = document.querySelector('.visualizer'); const mainSection = document.querySelector('.main-controls'); // disable stop button while not recording stop.disabled = true; // visualiser setup - create web audio api context and canvas let audioCtx; const canvasCtx = canvas.getContext("2d"); // 初始化WebSocket连接 window.addEventListener("DOMContentLoaded", () => { console.log('DOMContentLoaded done'); ws = new WebSocket("ws://localhost:8001/"); // 连接成功时标记就绪状态 ws.onopen = () => { console.log('WebSocket连接已建立'); isWsReady = true; }; // 处理连接错误 ws.onerror = (error) => { console.error('WebSocket连接出错:', error); isWsReady = false; }; // 处理连接关闭 ws.onclose = () => { console.log('WebSocket连接已关闭'); isWsReady = false; }; }) //main block for doing the audio recording if (navigator.mediaDevices.getUserMedia) { console.log('getUserMedia supported.'); const constraints = { audio: true }; let chunks = []; let onSuccess = function(stream) { const mediaRecorder = new MediaRecorder(stream); visualize(stream); record.onclick = function() { mediaRecorder.start(); console.log(mediaRecorder.state); console.log("recorder started"); record.style.background = "red"; stop.disabled = false; record.disabled = true; } stop.onclick = function() { mediaRecorder.stop(); console.log(mediaRecorder.state); console.log("recorder stopped"); record.style.background = ""; record.style.color = ""; stop.disabled = true; record.disabled = false; } mediaRecorder.onstop = function(e) { console.log("data available after MediaRecorder.stop() called."); const clipName = prompt('Enter a name for your sound clip?','My unnamed clip'); const clipContainer = document.createElement('article'); const clipLabel = document.createElement('p'); const audio = document.createElement('audio'); const deleteButton = document.createElement('button'); clipContainer.classList.add('clip'); audio.setAttribute('controls', ''); deleteButton.textContent = 'Delete'; deleteButton.className = 'delete'; if(clipName === null) { clipLabel.textContent = 'My unnamed clip'; } else { clipLabel.textContent = clipName; } clipContainer.appendChild(audio); clipContainer.appendChild(clipLabel); clipContainer.appendChild(deleteButton); soundClips.appendChild(clipContainer); audio.controls = true; const blob = new Blob(chunks, { 'type' : 'audio/ogg; codecs=opus' }); chunks = []; const audioURL = window.URL.createObjectURL(blob); audio.src = audioURL; console.log("recorder stopped"); // 发送音频Blob到服务器 if (isWsReady && ws.readyState === WebSocket.OPEN) { ws.send(blob); console.log('音频已发送到服务器'); } else { console.error('WebSocket未就绪,无法发送音频'); } deleteButton.onclick = function(e) { e.target.closest(".clip").remove(); } clipLabel.onclick = function() { const existingName = clipLabel.textContent; const newClipName = prompt('Enter a new name for your sound clip?'); if(newClipName === null) { clipLabel.textContent = existingName; } else { clipLabel.textContent = newClipName; } } } mediaRecorder.ondataavailable = function(e) { chunks.push(e.data); } } let onError = function(err) { console.log('The following error occured: ' + err); } navigator.mediaDevices.getUserMedia(constraints).then(onSuccess, onError); } else { console.log('getUserMedia not supported on your browser!'); } // 原visualize函数(MDN代码中未给出,需保留原实现) function visualize(stream) { if (!audioCtx) { audioCtx = new AudioContext(); } const source = audioCtx.createMediaStreamSource(stream); const analyser = audioCtx.createAnalyser(); analyser.fftSize = 2048; const bufferLength = analyser.frequencyBinCount; const dataArray = new Uint8Array(bufferLength); source.connect(analyser); draw() function draw() { const WIDTH = canvas.width; const HEIGHT = canvas.height; requestAnimationFrame(draw); analyser.getByteTimeDomainData(dataArray); canvasCtx.fillStyle = 'rgb(200, 200, 200)'; canvasCtx.fillRect(0, 0, WIDTH, HEIGHT); canvasCtx.lineWidth = 2; canvasCtx.strokeStyle = 'rgb(0, 0, 0)'; canvasCtx.beginPath(); let sliceWidth = WIDTH * 1.0 / bufferLength; let x = 0; for(let i = 0; i < bufferLength; i++) { let v = dataArray[i] / 128.0; let y = v * HEIGHT/2; if(i === 0) { canvasCtx.moveTo(x, y); } else { canvasCtx.lineTo(x, y); } x += sliceWidth; } canvasCtx.lineTo(canvas.width, canvas.height/2); canvasCtx.stroke(); } }
关键修改点说明
- 把
ws变量声明在全局作用域,让录音逻辑能访问到 - 增加
isWsReady标记,配合ws.readyState判断连接是否可用 - 在
mediaRecorder.onstop中生成Blob后,检查WebSocket状态再发送 - 补充了WebSocket的
onopen、onerror、onclose事件处理,便于调试连接状态
内容的提问来源于stack exchange,提问作者user16345050
相关产品推荐
相关产品推荐

