gpt4 book ai didi

javascript - AudioContext() 的多个来源

转载 作者:行者123 更新时间:2023-11-28 15:37:14 25 4
gpt4 key购买 nike

我有两个一秒音频源,如下所示:

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);

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

最佳答案

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);

关于javascript - AudioContext() 的多个来源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25239195/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com