Scheduling audio is something the Web Audio API was designed for. If you have the decoded PCM audio chunks as typed arrays (AUDIO_CHUNKS
), you can create audio buffers for each chunk, and schedule them at an exact time (one after the other) using noteOn()
. Something like:
var startTime = 0;
for (var i = 0, audioChunk; audioChunk = AUDIO_CHUNKS[i]; ++i) {
// Create/set audio buffer for each chunk
var audioBuffer = audioCtx.createBuffer(NUM_CHANNELS, NUM_SAMPLES, SAMPLE_RATE);
audioBuffer.getChannelData(0).set(audioChunk);
var source = audioCtx.createBufferSource();
source.buffer = audioBuffer;
source.noteOn(startTime);
source.connect(audioCtx.destination);
startTime += audioBuffer.duration;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…