我正在使用以下代码来解码来自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数据的示例吗?

此外,使用Web音频API处理实时流媒体的正确方法是什么?我已经阅读了几乎所有与此主题相关的问题,并且似乎没有任何问题可以解决。有任何想法吗?

最佳答案

您可以以以下代码为例:https://github.com/kmoskwiak/node-tcp-streaming-server

它基本上使用媒体源扩展。您需要做的就是从视频转换为音频
buffer = mediaSource.addSourceBuffer('audio/mpeg');

08-03 17:18