AudioContext()的多个来源

时间:2014-08-11 08:47:59

标签: javascript google-chrome firefox web-audio audiocontext

我有两个一秒钟的音频源如下:

var context = system.AudioContext();
var source = context.createBufferSource();

var audioBuffer1 = context.createBuffer(1, float32Array_1.length, context.sampleRate);
audioBuffer1.getChannelData(0).set(float32Array_1);

var audioBuffer2 = context.createBuffer(1, float32Array_2.length, context.sampleRate);
audioBuffer2.getChannelData(0).set(float32Array_2);

现在我想播放这两个音频源,它们之间没有延迟。对于单个源,我可以使用以下代码播放音频:

source.buffer = audioBuffer1;
source.connect(context.destination);
source.start(0);

如何附加第二个来源,以便它们之间没有延迟。

1 个答案:

答案 0 :(得分:4)

var context = system.AudioContext();
var source = context.createBufferSource();
var source2 = context.createBufferSource();

var audioBuffer1 = context.createBuffer(1, float32Array_1.length, context.sampleRate);
audioBuffer1.getChannelData(0).set(float32Array_1);

var audioBuffer2 = context.createBuffer(1, float32Array_2.length, context.sampleRate);
audioBuffer2.getChannelData(0).set(float32Array_2);

source.buffer = audioBuffer1;
source.connect(context.destination);

source2.buffer = audioBuffer2;
source2.connect(context.destination);

var time = context.currentTime;

source.start(time);
source2.start(time+audioBuffer1.duration);