HTML and CSS Reference
In-Depth Information
Listing 2-5. A Simplified Example of Loading an Audio File
/**
* soundName - The name of the sound to load
* getPreferredFormat - A function to determine the preferred format to use for a sound with a given
name.
* The algorithm for this decision is up to the game.
* audioContext - The audio context instance for the Web Audio API
* bufferCreated - The callback function for when the buffer has been successfully created
* bufferFailed - The callback function for the audio decode has failed
*/
// Check once at the start of the game which audio types are supported
var supported = {
ogg: false,
mp3: false,
wav: false,
mp4: false
};
var audio = new Audio();
if (audio.canPlayType('audio/ogg')) {
supported.ogg = true;
}
if (audio.canPlayType('audio/mp3')) {
supported.mp3 = true;
}
if (audio.canPlayType('audio/wav')) {
supported.wav = true;
}
if (audio.canPlayType('audio/mp4')) {
supported.mp4 = true;
}
// Audio element is thrown away after the support query
audio = null;
var soundPath = getPreferredFormat(soundName, supported);
var xhr = new window.XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
var xhrStatus = xhr.status;
var response = xhr.response;
Search WWH ::




Custom Search