作者:极御云安全 | 来源:互联网 | 2023-09-24 09:56
我通过web-Socket从服务器传输音频数据ws.on('message',functionincoming(message){varreadStreamfs
我通过web-Socket从服务器传输音频数据
ws.on('message', function incoming(message) {
var readStream = fs.createReadStream("angular/data/google.mp3",
{
'flags': 'r',
'highWaterMark': 128 * 1024
}
);
readStream.on('data', function(data) {
ws.send(data);
});
readStream.on('end', function() {
ws.send('end');
});
readStream.on('error', function(err) {
console.log(err)
});
});
在客户端
var chunks = [];
var cOntext= new AudioContext();
var soundSource;
var ws = new WebSocket(url);
ws.binaryType = "arraybuffer";
ws.Onmessage= function(message) {
if (message.data instanceof ArrayBuffer) {
chunks.push(message.data)
} else {
createSoundSource(chunks);
}
};
function createSoundSource(audioData) {
soundSource = context.createBufferSource();
for (var i=0; i context.decodeAudioData(audioData[i], function(soundBuffer){
soundSource.buffer = soundBuffer;
soundSource.connect(context.destination);
soundSource.start(0);
});
}
}
但是设置缓冲区soundSource.buffer = soundBuffer;第二次导致错误
Uncaught DOMException: Failed to set the ‘buffer’ property on ‘AudioBufferSourceNode’: Cannot set buffer after it has been already been set
任何有关如何最好地使用新音频数据更新Web Audio API播放的建议或见解将不胜感激.
解决方法:
不是积极的,但我认为你必须处理你的流式websocket缓冲区有点不同.也许websocket-streaming-audio软件包源代码可以为您提供有关如何处理方案的更好线索.