Skip to content

Instantly share code, notes, and snippets.

@moduscreate
Created April 2, 2012 21:44
Show Gist options
  • Save moduscreate/2287453 to your computer and use it in GitHub Desktop.
Save moduscreate/2287453 to your computer and use it in GitHub Desktop.
HTML5-iOS/ImpactJS Sound Effect sprite (Prototype)
var SoundSprite = function(src, schedule) {
var me = this;
me.src = src;
me.schedule = schedule;
me.init();
};
SoundSprite.prototype = {
currentIndex : 0,
numSounds : 2,
setVolume : function(volume) {
this.audio.volume = volume;
},
init : function() {
var me = this,
i = 0,
audio = new Audio(),
src;
// Probe sound formats and determine the file extension to load
for(; i < ig.Sound.use.length; i++ ) {
var format = ig.Sound.use[i];
if( audio.canPlayType(format.mime) ) {
this.format = format;
break;
}
}
if( !this.format ) {
ig.Sound.enabled = false;
return;
}
src = me.src.match(/^(.*)\.[^\.]+$/)[1] + '.' + this.format.ext + ig.nocache;
me.audio = audio;
audio.src = src;
audio.play();
audio.addEventListener('timeupdate', Ext.Function.bind(me.onAudioTimeUpdate, me));
},
play : function(sound) {
var me = this,
region = me.schedule[sound],
audio = me.audio;
if (audio.currentTime > 0) {
audio.pause();
}
audio.currentTime = region[0];
me.endTime = region[1];
audio.play();
},
onAudioTimeUpdate : function(evt) {
var me = this,
audio = me.audio;
if (me.endTime && audio.currentTime > me.endTime) {
audio.pause();
delete me.endTime;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment