Permalink
Browse files

redone events to allow binding after init method and allow for multip…

…le bindings to a single event
  • Loading branch information...
tommoor committed Apr 18, 2011
1 parent a312f76 commit 4f55794b3739f8d325f678d038b62d32e1242e11
Showing with 98 additions and 64 deletions.
  1. +27 −10 Readme.md
  2. +54 −31 Zap.js
  3. +1 −1 Zap.min.js
  4. +5 −7 examples/click.html
  5. +5 −7 examples/group.html
  6. +6 −8 examples/loop.html
View
@@ -1,6 +1,6 @@
# Zap! HTML 5 Game Audio
-Zap! is an audio manager for html 5 applications with a focus on real time uses such as games.
+Zap! is an audio manager for html 5 applications with a focus on real-time uses such as games.
It provides a common interface that works across the individual browser quirks and shortcomings such as multiple channels, looping and callbacks.
@@ -21,18 +21,35 @@ __Example__
Zap.init({
console: true,
- complete: function(){
- // callback for all sounds finished loading
- },
- error: function(ref){
- // callback when a sound fails to load
- },
- update: function(percent){
- // callback when a sound finishes loading
- }
+ container: 'sound-holder'
});
+### on(event, callback)
+
+There are currently three events that can be bound using the 'on' function; 'success', 'update' and 'error'.
+Multiple callbacks may also be bound.
+
+__Arguments__
+
+* ref - An event (success, update or error)
+* callback - A function to be called whenever the above event occurs
+
+__Example__
+
+ Zap.on('complete', function(){
+ // all sounds are loaded
+ });
+
+ Zap.on('update', function(percent){
+ // sounds are loading
+ });
+
+ Zap.on('error', function(ref){
+ // there was a problem loading a sound file
+ });
+
+
### addSound(ref, sources, channels, time)
View
85 Zap.js
@@ -9,15 +9,13 @@ var Zap = (function() {
var exports = {},
sounds = {},
groups = {},
+ events = {},
loaded = 0,
timeout = 6*1000,
self = this,
options = {
container: 'sounds',
- console: true,
- complete: function(){},
- error: function(){},
- update: function(){}
+ console: true
};
@@ -45,6 +43,31 @@ var Zap = (function() {
}
+ /**
+ * Event emitter hook
+ *
+
+ * @param {String} event
+ * @param {Function} callback to trigger on event
+ * @return {Zap} return itself to allow chaining
+ */
+ exports.on = function(event, callback){
+
+ if(! events[event]) events[event] = [];
+ events[event].push(callback);
+ return this;
+ }
+
+
+ var trigger = function(event){
+ var callbacks = events[event];
+
+ for(var c in callbacks){
+ callbacks[c].apply( this, Array.prototype.slice.call( arguments, 1 ) );
+ }
+ }
+
+
/**
* Allows sounds to be grouped under one reference, so that a random sound can be played
@@ -91,8 +114,8 @@ var Zap = (function() {
log('Zap: sound failed to load: ' + ref);
sounds[ref].failed = true;
- soundLoaded();
- options.error(ref);
+ soundLoaded();
+ trigger('error', ref);
}, time || timeout);
@@ -136,15 +159,15 @@ var Zap = (function() {
}
- /**
- * Play a sound by reference
- *
- * @param {String} ref
- * @param {Float} vol
- * @param {Integer} loops
- * @param {Function} callback
- * @return {Zap} return itself to allow chaining
- */
+ /**
+ * Play a sound by reference
+ *
+ * @param {String} ref
+ * @param {Float} vol
+ * @param {Integer} loops
+ * @param {Function} callback
+ * @return {Zap} return itself to allow chaining
+ */
exports.play = function(ref, vol, loops, c){
@@ -250,27 +273,27 @@ var Zap = (function() {
var a = document.createElement('audio');
// MP3
- this.formats.mp3 = !!(a.canPlayType && a.canPlayType('audio/mpeg;').replace(/no/, ''));
-
- // Vorbis
- this.formats.vorbis = !!(a.canPlayType && a.canPlayType('audio/ogg; codecs="vorbis"').replace(/no/, ''));
-
- // WAV
- this.formats.wav = !!(a.canPlayType && a.canPlayType('audio/wav; codecs="1"').replace(/no/, ''));
-
- // AAC
- this.formats.aac = !!(a.canPlayType && a.canPlayType('audio/mp4; codecs="mp4a.40.2"').replace(/no/, ''));
-
- return this.formats;
+ this.formats.mp3 = !!(a.canPlayType && a.canPlayType('audio/mpeg;').replace(/no/, ''));
+
+ // Vorbis
+ this.formats.vorbis = !!(a.canPlayType && a.canPlayType('audio/ogg; codecs="vorbis"').replace(/no/, ''));
+
+ // WAV
+ this.formats.wav = !!(a.canPlayType && a.canPlayType('audio/wav; codecs="1"').replace(/no/, ''));
+
+ // AAC
+ this.formats.aac = !!(a.canPlayType && a.canPlayType('audio/mp4; codecs="mp4a.40.2"').replace(/no/, ''));
+
+ return this.formats;
}
+
var getChannelCount = function(){
var c = 0;
for (var i in sounds) {
c += sounds[i].channels;
- }
-
+ }
return c;
}
@@ -280,9 +303,9 @@ var Zap = (function() {
var soundLoaded = function(){
loaded++;
- options.update( getPercentageLoaded() );
+ trigger('update', getPercentageLoaded() );
- if(loaded == getChannelCount()) options.complete();
+ if(loaded == getChannelCount()) trigger('complete');
}
var log = function(){
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -6,13 +6,11 @@
<script src="../Zap.js"></script>
<script>
$(function(){
- Zap.init({
- complete: function(){
- $(window).click(function(){
-
- Zap.play('laser');
- });
- }
+ Zap.init();
+ Zap.on('complete', function(){
+ $(window).click(function(){
+ Zap.play('laser');
+ });
});
Zap.addSound('laser', ['zap.mp3', 'zap.ogg'], 5);
});
View
@@ -6,13 +6,11 @@
<script src="../Zap.js"></script>
<script>
$(function(){
- Zap.init({
- complete: function(){
-
- $(window).click(function(){
- Zap.play('zappingbeats');
- });
- }
+ Zap.init();
+ Zap.on('complete', function(){
+ $(window).click(function(){
+ Zap.play('zappingbeats');
+ });
});
Zap.addGroup('zappingbeats', ['zap', 'beat']);
View
@@ -6,15 +6,13 @@
<script src="../Zap.js"></script>
<script>
$(function(){
- Zap.init({
- complete: function(){
- Zap.play('beat', 1, 5, function(){
-
- alert('loop finished!');
- });
- }
+ Zap.init();
+ Zap.on('complete', function(){
+ Zap.play('beat', 1, 5, function(){
+
+ alert('loop finished!');
+ });
});
-
Zap.addSound('beat', ['beat.mp3', 'beat.ogg'], 1);
});
</script>

0 comments on commit 4f55794

Please sign in to comment.