【发布时间】:2017-12-01 19:43:47
【问题描述】:
我正在使用以下代码从 nodejs 的套接字解码音频块
window.AudioContext = window.AudioContext || window.webkitAudioContext;
var context = new AudioContext();
var delayTime = 0;
var init = 0;
var audioStack = [];
var nextTime = 0;
client.on('stream', function(stream, meta){
stream.on('data', function(data) {
context.decodeAudioData(data, function(buffer) {
audioStack.push(buffer);
if ((init!=0) || (audioStack.length > 10)) { // make sure we put at least 10 chunks in the buffer before starting
init++;
scheduleBuffers();
}
}, function(err) {
console.log("err(decodeAudioData): "+err);
});
});
});
function scheduleBuffers() {
while ( audioStack.length) {
var buffer = audioStack.shift();
var source = context.createBufferSource();
source.buffer = buffer;
source.connect(context.destination);
if (nextTime == 0)
nextTime = context.currentTime + 0.05; /// add 50ms latency to work well across systems - tune this if you like
source.start(nextTime);
nextTime+=source.buffer.duration; // Make the next buffer wait the length of the last buffer before being played
};
}
但它在音频块之间有一些我无法弄清楚的间隙/故障。
我还了解到,使用 MediaSource 可以执行相同的操作,并由播放器处理时间,而不是手动执行。有人可以提供一个处理 mp3 数据的例子吗?
此外,使用网络音频 API 处理实时流媒体的正确方法是什么?我已经阅读了几乎所有关于这个主题的问题,而且似乎没有一个问题没有故障。有什么想法吗?
【问题讨论】:
标签: node.js sockets audio web-audio-api