BinaryJs + Audio API调度数组缓冲区

时间:2014-06-22 23:33:47

标签: javascript node.js api audio web

我有一个node.js服务器流式传输一些ArrayBuffers:

var BinaryServer = require('binaryjs').BinaryServer;
var fs = require('fs');

var server = BinaryServer({port: 2000});

server.on('connection', function(client){
  var file = fs.createReadStream(__dirname + '/music/02 Enter Sandman.mp3');
  client.send(file);
});

在客户端,我正在听那些块并试图一个接一个地播放它们:

var client = new BinaryClient('ws://localhost:2000');

var context = new AudioContext();

var source = context.createBufferSource();
source.connect(context.destination);

var audioStart = 0;
var audioStop = 0;

client.on('stream', function(stream, meta)
{
    stream.on('data', function(data)
    {
        var audio = new Float32Array(data);
        var audioBuffer = context.createBuffer(1, audio.length, 44100);

        audioBuffer.getChannelData(0).set(audio);

        audioBuffer = context.createBuffer(data, true);

        audioStop += audioBuffer.duration;

        source = context.createBufferSource();
        source.connect(context.destination);
        source.buffer = audioBuffer;
        source.start(audioStart);
        //source.stop(audioStop);

        console.log(audioStart + " - " + audioStop);

        audioStart += audioBuffer.duration;
    });
});

我经常收到此错误:Uncaught InvalidStateError: Failed to execute 'start' on 'AudioBufferSourceNode': cannot call start more than once.

只有第一个块播放

有人可以解释一下这应该如何运作吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

Web Audio API出现问题,您无法在同一频道中加载两个或多个来源。

您可以将来源与:source.disconnect();断开连接 并重新与他联系:source.connect(context.destination);

此处提供更多信息:https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API