Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
html5 audio queue
<!doctype html>
//This plays a file, and call a callback once it completed (if a callback is set)
function play(audio, callback) {;
if (callback) {
//When the audio object completes it's playback, call the callback
audio.addEventListener('ended', callback);
//Changed the name to better reflect the functionality
function play_sound_queue(sounds) {
var index = 0;
function recursive_play() {
//If the index is the last of the table, play the sound
//without running a callback after
if (index + 1 === sounds.length) {
play(sounds[index], null);
} else {
//Else, play the sound, and when the playing is complete
//increment index by one and play the sound in the
//indexth position of the array
play(sounds[index], function() {
//Call the recursive_play for the first time
function play_all() {
play_sound_queue([new Audio(""), new Audio(""), new Audio("")]);
<a href="javascript:;" onclick="play_all();">PLAY THIS!</a>
Copy link

yuraloginoff commented Dec 10, 2020

No license or copyright needed. Free for commercial and personal usage.

Copy link

ghost commented Sep 30, 2021

Works great! It seems that there's a noticeable delay (1-2 seconds) before the next sound start's playing though. Do you know what could be causing this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment