1 /**	@class Provides a way to load and play back .wav file sound effects.
  2 	@description Load chunks of audio and play them by name. Only .wav files are really supported on all browsers. You could try .mp3 or .ogg but they probably won't work on all browsers. There is no way to get progress feedback on whether sounds are loaded yet, sorry, so use short sounds. Google 'sfxr' for an excellent sound effects generator.
  3 */
  4 //	http://stackoverflow.com/questions/3652005/hearing-an-echo-while-playing-mp3
  5 function AudioManager() {
  6 	var samples = {};
  7 	
  8 	/**
  9 		Load a sample to play later.
 10 		@param url is the path to the file to load.
 11 		@param name is optional and is used to identify your sample for playing. If not supplied the filename minus the extension will be used.
 12 	*/
 13 	this.load = function(url, name) {
 14 		if (arguments.length == 1) {
 15 			name = url.split("/").slice(-1)[0].split(".").slice(0, -1).join(".");
 16 		}
 17 		if (typeof Audio == "undefined") {
 18 			var em = document.createElement('embed');
 19 			if( em != null ) {
 20 				// var embed=document.createElement('object');
 21 				// embed.setAttribute('type','audio/wav');
 22 				// embed.setAttribute('data', 'c:\test.wav');
 23 				// embed.setAttribute('autostart', true);
 24 				//document.getElementsByTagName('body')[0].appendChild(embed);
 25 				
 26 				// TODO:
 27 				// .addEventListener('load',foo,false);
 28 				// .attachEvent('load',foo,false);
 29 				
 30 				em.setAttribute("width","0");
 31 				em.setAttribute("height","0");
 32 				em.setAttribute("hidden","true");
 33 				em.setAttribute("autostart", false);
 34 				em.setAttribute("enablejavascript", true);
 35 				// argh none of these work in IE6 :(
 36 				//em.setAttribute("onload", function() { alert('hi'); });
 37 				//em.onload = function() { alert("loaded: " + url);};
 38 				//em.onloadeddata = function() { alert("loaded: " + url);};
 39 				//em.attachEvent("onload", function() { alert("xxx"); });
 40 				em.setAttribute("src", url);
 41 				em._play = function() { em.Play(); };
 42 				document.body.appendChild(em);
 43 				samples[name] = em;
 44 			}
 45 		// see if phonegap is available to play sound on Android
 46 		} else if (window["PhoneGap"] && window["device"] && device.platform == "Android") {
 47 			var au = new Media("/android_asset/www/" + url);
 48 			au._play = function() {	au.play(); };
 49 			samples[name] = au;
 50 		} else {
 51 			var au = new Audio();
 52 			/*console.log(au.canPlayType("audio/mpeg"));
 53 			console.log(au.canPlayType("audio/ogg"));
 54 			console.log(au.canPlayType("audio/wav"));*/
 55 			
 56 			// argh none of these work in webkit :(
 57 			// firefox works fine though
 58 			
 59 			// TODO: try "loaded" and "load" as event names
 60 			
 61 			//au.onload = function() { alert("onload: " + url);};
 62 			//au.onloadeddata = function() { alert('onloadeddata: ' + url); };
 63 			//au.oncanplay = function() { alert('oncanplay: ' + url); };
 64 			//au.addEventListener("onloadeddata", function() { alert('yes'); }, false);
 65 			//au.addEventListener("onload", function() { alert('yes'); }, false);
 66 			//au.onreadystatechange = function() { alert('p'); };
 67 			au.src = url;
 68 			//au.load();
 69 			// sneaky multi-channel playing hack
 70 			au._play = function() {
 71 				var x = new Audio();
 72 				x.src = au.src;
 73 				x.play();
 74 			};
 75 			samples[name] = au;
 76 		}
 77 	}
 78 	
 79 	/**
 80 		Play a sound.
 81 		@param name identifies which sound to play.
 82 	*/
 83 	this.play = function(name) {
 84 		if (samples[name]) {
 85 			samples[name]._play();
 86 		} else {
 87 			if (typeof console != "undefined") {
 88 				console.log("No such sound: " + name);
 89 			} else {
 90 				alert("No such sound: " + name);
 91 			}
 92 		}
 93 	}
 94 }
 95