Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
577 views
in Technique[技术] by (71.8m points)

javascript - Web Audio API:如何播放MP3块流(Web Audio API: How to play a stream of MP3 chunks)

So I'm trying to use Web Audio API to decode & play MP3 file chunks streamed to the browser using Node.js & Socket.IO.(因此,我尝试使用Web Audio API来解码和播放使用Node.js和Socket.IO流向浏览器的MP3文件块。)

Is my only option, in this context, to create a new AudioBufferSourceNode for each audio data chunk received or is it possible to create a single AudioBufferSourceNode for all chunks and simply append the new audio data to the end of source node's buffer attribute?(在这种情况下,我唯一的选择是为每个接收到的音频数据块创建一个新的AudioBufferSourceNode ,还是可以为所有块创建一个AudioBufferSourceNode并简单地将新音频数据附加到源节点的buffer属性的末尾?)

Currently this is how I'm receiving my MP3 chunks, decoding them and scheduling them for playback.(目前,这就是我接收MP3块,对其进行解码并安排它们进行播放的方式。)

I have already verified that each chunk being received is a 'valid MP3 chunk' and is being successfully decoded by the Web Audio API.(我已经验证了接收到的每个块都是“有效的MP3块”,并且已被Web Audio API成功解码。)
audioContext = new AudioContext();
startTime = 0;

socket.on('chunk_received', function(chunk) {
    audioContext.decodeAudioData(toArrayBuffer(data.audio), function(buffer) {
        var source = audioContext.createBufferSource();
        source.buffer = buffer;
        source.connect(audioContext.destination);

        source.start(startTime);
        startTime += buffer.duration;
    });
});

Any advice or insight into how best to 'update' Web Audio API playback with new audio data would be greatly appreciated.(对于如何最好地用新的音频数据“更新” Web Audio API回放的任何建议或见解,将不胜感激。)

  ask by Jonathan Byrne translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No, you can't reuse an AudioBufferSourceNode, and you cant push onto an AudioBuffer.(不,您不能重用AudioBufferSourceNode,也不能push送到AudioBuffer。)

Their lengths are immutable.(它们的长度是不变的。)

This article ( http://www.html5rocks.com/en/tutorials/audio/scheduling/ ) has some good information about scheduling with the Web Audio API.(本文( http://www.html5rocks.com/en/tutorials/audio/scheduling/ )提供了一些有关使用Web Audio API进行计划的信息。)

But you're on the right track.(但是您走在正确的轨道上。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...