Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added basic playlist support #57

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 44 additions & 5 deletions lib/quintus_audio.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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
Expand All @@ -69,14 +100,17 @@ Quintus.Audio = function(Q) {
} else {
setTimeout(function() {
Q.audio.removeSound(soundID);
if(typeof callback !== 'undefined') {
callback();
}

},source.buffer.duration * 1000);
}
source.assetName = s;
if(source.start) { source.start(0); } else { source.noteOn(0); }

Q.audio.playingSounds[soundID] = source;


};

Q.audio.stop = function(s) {
Expand All @@ -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
Expand All @@ -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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong. The callback shouldn't be invoked when passed to the event listener. Also listener should be removed after use. Should look like this:

        var endedListener = function(){
          Q.audio.channels[i].channel.removeEventListener('ended', endedListener);  
          callback();
        }
        Q.audio.channels[i].channel.addEventListener('ended', endedListener);

}


// If we're looping - just set loop to true to prevent this channcel
// from being used.
Expand Down Expand Up @@ -152,6 +191,6 @@ Quintus.Audio = function(Q) {
};

};

};