I have a task in Javascript that requires continuous execution. I have a list of audio files stored as buffers (audioBuffer), I wish to play. However there are audio files being constantly appended to that list, so I use a while loop to monitor it. I constantly get the first buffer, play its audio, and dequeue it. However, when I run this function, it makes my browser hang, even though the loop is in an async wrapper. Why is this? Shouldn't async largely prevent my browser from freezing?
Code:
function playAudioQueue() {
const player = new Audio();
(async () => {
while (true) {
const audioBuffer = audioQueue[0];
if (audioBuffer) {
const base64Audio = base64Prefix + arrayBufferToBase64(audioBuffer);
player.src = base64Audio;
await player.play();
audioQueue.shift();
};
};
})();
};