带有AudioContext的Socket.io在接收时发送和接收audio错误

我正在尝试构build一些东西,用户可以使用socket.io,audioContext,js作为前端,Node.js,socket.io作为服务器,让用户可以即时发送audio给很多人。

我可以录制audio,将其发送到服务器并将其发回给其他用户,但我无法播放这些数据。 我想这肯定是我发送它们的问题,或者我如何处理接收它们的缓冲区。

我得到以下错误: 更新!

传递给decodeAudioData的缓冲区包含未知的内容types。

audio传递正常,缓冲区没有错误地创build,但没有声音反馈。

用户按下logging,并开始logging/stream与他以下function:

这是如何开始的:

navigator.getUserMedia({audio: true,video: false}, initializeRecorder, errorCallback); function initializeRecorder(stream) { var bufferSize = 2048; audioCtx = new (window.AudioContext || window.webkitAudioContext)(); var source = audioCtx.createMediaStreamSource(stream); var recorder = audioCtx.createScriptProcessor(bufferSize, 1, 1); recorder.onaudioprocess = recorderProcess; source.connect(recorder); recorder.connect(audioCtx.destination); recording = true; initialized = true; play = false; stop = true; } function recorderProcess(e) { var left = e.inputBuffer.getChannelData(0); socket.emit('audio-blod-send', convertFloat32ToInt16(left)); } function convertFloat32ToInt16(buffer) { l = buffer.length; buf = new Int16Array(l); while (l--) { buf[l] = Math.min(1, buffer[l])*0x7FFF; } return buf.buffer; } 

然后,服务器使用该套接字来广播原始发件人发送的内容:

 socket.on('audio-blod-send',function(data){ socket.broadcast.to(roomName).emit('audio-blod-receive', data); }); 

然后数据被播放: 更新! 我正在使用audioContext.decodeData,我发现它只用于从MP3或WAV文件读取/解码audio不stream。 随着新的代码没有错误出现,但没有audio反馈。

  socket.on('audio-blod-receive',function(data) { playAudio(data); }); function playAudio(buffer) { var audioCtx; var started = false; if(!audioCtx) { audioCtx = new (window.AudioContext || window.webkitAudioContext)(); } source = audioCtx.createBufferSource(); audioBuffer = audioCtx.createBuffer( 1, 2048, audioCtx.sampleRate ); audioBuffer.getChannelData( 0 ).set( buffer ); source.buffer = audioBuffer; source.connect( audioCtx.destination ); source.start(0); console.log(buffer); } 

PS如果有人对我正在尝试做的事感兴趣,请随时与我联系。