Web Audio API:如何播放MP3块

Jon*_*rne 20 html javascript audio streaming web-audio-api

因此,我正在尝试Web Audio API使用Node.js和Socket.IO解码和播放流式传输到浏览器的MP3文件块.

我的问题是我唯一的选择是AudioBufferSourceNode为每个接收到的音频数据块创建一个新的,还是可以为所有块创建一个AudioBufferSourceNode,只需将新的音频数据附加到源节点AudioBufferSourceNode属性的末尾?

目前,我正在接收我的MP3块,解码并安排它们进行播放.我已经验证了收到的每个块都是一个"有效的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;
    });
});
Run Code Online (Sandbox Code Playgroud)

任何有关如何使用新音频数据"更新"Web音频API播放的建议或见解将不胜感激.

Ant*_*ris 7

目前,decodeAudioData()需要完整的文件,不能对不完整的文件提供基于块的解码。下一个版本的 Web Audio API 应该提供这个功能:https : //github.com/WebAudio/web-audio-api/issues/337

同时,在新的 API 版本可用之前,我已经开始编写以块为单位解码音频的示例。

https://github.com/AnthumChris/fetch-stream-audio


Kev*_*nis 5

不,你不能重复使用AudioBufferSourceNode,而且你无法push使用AudioBuffer.它们的长度是不变的.

本文(http://www.html5rocks.com/en/tutorials/audio/scheduling/)提供了有关使用Web Audio API进行调度的一些很好的信息.但是你走在正确的轨道上.