浏览器录音经socket.io传Node.js后回放速率异常,疑华硕D1声卡兼容问题
Hey folks, I ran into a weird audio glitch recently and wanted to share my solution in case anyone else gets stuck with the same problem.
Here's what was happening: I was recording audio in the browser using MediaRecorder, sending the generated Blob to a Node.js server via Socket.io, and saving the audio either to a MySQL database or as an .ogg file. But whenever I played back the audio—whether pulling it from the database or the saved file—I kept getting strange distortion: the sound would suddenly speed up at random intervals, then jump back to normal speed. I scoured through forums and documentation but couldn't find a fix at first.
Update: After testing across multiple devices, I realized this wasn't a code or server-side issue at all—it's a hardware/software compatibility problem with my Asus D1 sound card. When I recorded audio on other devices (like a laptop with built-in speakers or a different external audio interface), the playback was completely normal with no distortion.
For reference, here's the code I was using for recording and sending the audio:
var mediaRecorder; var startRecord = function(recordData){ if (!navigator.getUserMedia) navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia; if (navigator.getUserMedia) { navigator.getUserMedia({audio:true}, success, function(stream) { alert('Error capturing audio.'); }); } else alert('getUserMedia not supported in this browser.'); function success(stream) { var chunks = []; mediaRecorder = new MediaRecorder(stream); mediaRecorder.start(); mediaRecorder.onstop = function(e) { stream.getAudioTracks()[0].stop() var blob = new Blob(chunks, { 'type' : 'audio/ogg; codecs=opus' }) var audioURL = URL.createObjectURL(blob); if (recordData.id) { socket.emit('audio', { 'id':recordData.id, 'audio':blob, 'option':recordData.type }); } mediaRecorder.ondataavailable = function(e) { chunks.push(e.data); } } }; var stopRecord = function(recordData){ mediaRecorder.stop(); mediaRecorder.state = 'recording' };
A quick takeaway: If you're dealing with unexpected audio playback issues, don't rule out hardware-specific quirks! Sometimes the problem isn't in your code, but in how your device's audio interface interacts with the browser's recording APIs.
内容的提问来源于stack exchange,提问作者MikeyB

