HTML and CSS Reference
In-Depth Information
if (xhrStatus === 200) {
if (audioContext.decodeAudioData) {
audioContext.decodeAudioData(response, bufferCreated, bufferFailed);
} else {
var buffer = audioContext.createBuffer(response, false);
xhr.onreadystatechange = null;
xhr = null;
};'GET', soundPath, true);
xhr.responseType = 'arraybuffer';
In Listing 2-5, the getPreferredFormat function is specified by the game to best decide which format to use. The
choice should be based on the knowledge the game has about the required sound and how it is used. For example, if
the file is a short sound effect (a few seconds in length) that needs to be played quickly, such a bullet fire sound, then
selecting an uncompressed WAV file might be the best option, because unlike most MP3/OGG/AAC files it doesn't
need to be decoded before use. It depends on the size and length of the sound. This example is unlikely to hold true
for music files, which are usually minutes, not seconds, in length and hence are much larger when uncompressed.
Since the Web Audio API requires compressed audio files to be decompressed before use, storage of the
uncompressed audio can quickly become an issue, especially on mobile where resources are more limited.
Depending on the hardware, this decoding can be expensive for large files, so loading all large data files, such as
music, upfront and then decoding is considered a bad idea. One option is to only load the music immediately
required and wait until later to load other music.
If the sound needs to be played immediately, then an alternative approach is required for larger audio files. It
is possible to combine streaming HTML5 Audio sounds with the Web Audio API, but at the time of writing, support
is limited and unstable but should get better with improvements to media streaming in the future. In the absence of
such features, one possible approach to speed up loading times is to load a smaller lower quality version of an audio
file and start playing, only to replace it with a higher quality version after it has been loaded. The ability to seek and
cross-fade two sources in the Web Audio API should make this a seamless transition. This type of functionality could
be written at an audio library level. Figure 2-4 shows an example of loading and playing audio data with respect to
user-driven events within the game.
Search WWH ::

Custom Search