topical media & game development
mobile-game-ch25-quintus-audio.js / js
Quintus.Audio = function(Q) {
Q.audio = {
channels: [],
channelMax: Q.options.channelMax || 10,
active: {}
};
Q.enableSound = function() {
var hasTouch = !!('ontouchstart' in window);
if(!hasTouch) {
Q.audio.enableDesktopSound();
} else {
Q.audio.enableMobileSound();
}
return Q;
};
// Dummy methods
Q.play = function() {};
Q.audioSprites = function() {}
Q.audio.enableDesktopSound = function() {
for (var i=0;i<Q.audio.channelMax;i++) {
Q.audio.channels[i] = {};
Q.audio.channels[i]['channel'] = new Audio();
Q.audio.channels[i]['finished'] = -1;
}
Q.play = function(s,debounce) {
if(Q.audio.active[s]) return;
if(debounce) {
Q.audio.active[s] = true
setTimeout(function() {
delete Q.audio.active[s];
},debounce);
};
for (var i=0;i<Q.audio.channels.length;i++) {
var now = new Date();
if (Q.audio.channels[i]['finished'] < now.getTime()) {
Q.audio.channels[i]['finished'] = now.getTime() + Q.asset(s).duration*1000;
Q.audio.channels[i]['channel'].src = Q.asset(s).src;
Q.audio.channels[i]['channel'].load();
Q.audio.channels[i]['channel'].play();
break;
}
}
}
}
Q.audio.enableMobileSound = function() {
var isiOS = navigator.userAgent.match(/iPad|iPod|iPhone/i) != null;
Q.audioSprites = function(asset) {
if(_.isString(asset)) asset = Q.asset(asset);
Q.audio.spriteFile = asset['resources'][0].replace(/\.[a-z]+/,"");
if(Q.audio.sprites && Q.audio.sprites[sound]) {
var startTime = Q.audio.sprites[sound].start - 0.05,
endDelay = Q.audio.sprites[sound].end - startTime;
Q.audio.sheet.currentTime = startTime;
Q.audio.sheet.play();
clearTimeout(Q.audio.silenceTimer);
Q.audio.silenceTimer = setTimeout(Q.audio.timer,endDelay*1000 + 500);
}
};
}
};
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.