HTML and CSS Reference
In-Depth Information
if(!Q.audio.sheet || !Q.audio.silenceTimer) return;
if(Q.audio.activeSound) return;
if(debounce) {
Q.activeSound = true
setTimeout(function() {
Q.audio.activeSound = null;
},debounce);
}
sound = sound.replace(/\.[a-z0-9]+$/,"");
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);
}
};
};
This method first checks if the device is an iOS device using
userAgent
matching. This is not an ideal
method (user agent matching never is) but it's the only way to put in a specific file-type workaround for iOS.
Next is the
Q.audioSprites
method, which is used to pass in the JSON data asset that provides position
and length information about the sprites. It stores the filename to load and the sprites inside of the
Q.audio
object. It also binds to the first touch event on
Q.el
(the Canvas element) to start the audio system. This is the
trick that allows the system to start playing the sprite sound.
Next, it sets
Q.options.sound
to
false
, which has the effect of telling the loading system in Quintus
not to try to load any sound files in the normal
Q.load
process. This needs to be done to prevent the engine
from trying to load sound files because these files will never trigger their
canplaythrough
callbacks due to
mobile preloading restrictions.
The
Q.audio.timer
method is the default callback that ensures that when no other sounds are playing,
the audio element continues to loop over the first 500 milliseconds of the sprite, which are known to be silent.
The
Q.audio.start
method is called on the first touch event. Because it's triggered by a user event, it
can set up and load a sound file. When the
canplaythrough
event triggers, it calls play and starts the silence
timer.
Next, the system loads the sound file into the audio spritesheet at
Q.audio.sheet
. On iOS devices, al-
though mobile Safari has support for a number of different sound formats, only the
.caf
file format (when
encoded with the IMA-ADPCM codec) can be played natively without using iTunes to do the actual playing.
Using iTunes to play sounds causes a noticeable delay and stutter inside of mobile Safari.
Finally,
Q.audio.start
turns off the
touchstart
callback to prevent it from being called twice.
After handling debouncing much in the same way as on the desktop, the
Q.play
method itself has the job to
find the sprite in the
Q.audio.sprites
object. It first removes any file extensions off the sound name (the