diff --git a/lib/quintus_audio.js b/lib/quintus_audio.js index 66e1e589..6f9dc884 100644 --- a/lib/quintus_audio.js +++ b/lib/quintus_audio.js @@ -30,6 +30,37 @@ Quintus.Audio = function(Q) { } return Q; }; + + // Play a list of sounds. + Q.audio.playlist = function(playlist,options) { + var playlistIndex = 0; + + //shuffle playlist if requested + if(options && options['shuffle']) { + playlist = playlist.sort(function() { + return 0.5 - Math.random(); + }); + } + + //play first sound + if (typeof playlist[playlistIndex] !== 'undefined') { + Q.audio.play(playlist[playlistIndex], options, function() { + Q.audio.playNextTitle(playlist, playlistIndex, options); + }); + } + }; + + Q.audio.playNextTitle = function(playlist, playlistIndex, options) { + //increase playlist index + playlistIndex++; + + //if next sound exists, play it + if(typeof playlist[playlistIndex] !== 'undefined') { + Q.audio.play(playlist[playlistIndex], options, function() { + Q.audio.playNextTitle(playlist, playlistIndex, options); + }); + } + }; Q.audio.enableWebAudioSound = function() { Q.audio.type = "WebAudio"; @@ -41,10 +72,10 @@ Quintus.Audio = function(Q) { Q.audio.removeSound = function(soundID) { delete Q.audio.playingSounds[soundID]; }; - + // Play a single sound, optionally debounced // to prevent repeated plays in a short time - Q.audio.play = function(s,options) { + Q.audio.play = function(s,options, callback) { var now = new Date().getTime(); // See if this audio file is currently being debounced, if @@ -69,6 +100,10 @@ Quintus.Audio = function(Q) { } else { setTimeout(function() { Q.audio.removeSound(soundID); + if(typeof callback !== 'undefined') { + callback(); + } + },source.buffer.duration * 1000); } source.assetName = s; @@ -76,7 +111,6 @@ Quintus.Audio = function(Q) { Q.audio.playingSounds[soundID] = source; - }; Q.audio.stop = function(s) { @@ -101,7 +135,7 @@ Quintus.Audio = function(Q) { // Play a single sound, optionally debounced // to prevent repeated plays in a short time - Q.audio.play = function(s,options) { + Q.audio.play = function(s,options,callback) { var now = new Date().getTime(); // See if this audio file is currently being debounced, if @@ -122,6 +156,11 @@ Quintus.Audio = function(Q) { if (!Q.audio.channels[i]['loop'] && Q.audio.channels[i]['finished'] < now) { Q.audio.channels[i]['channel'].src = Q.asset(s).src; + + if(typeof callback !== 'undefined') { + Q.audio.channels[i].channel.addEventListener('ended', callback()); + } + // If we're looping - just set loop to true to prevent this channcel // from being used. @@ -152,6 +191,6 @@ Quintus.Audio = function(Q) { }; }; - + };