Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var MediaMaster = new (function(){
- var sounds = {};// a container of Audio instances and names(keys) as a reference to their respective values.
- // Example: sounds = {"jumpSound":new Audio('path')};
- // Master Controll of sounds
- var liveSounds = [],
- instanceVolume = {},
- globalVolume = 1;
- this.soundBatch = this.sBatch = {muted: false};
- // Hide global controls
- (function(SB){
- function killSound(snd){
- snd.pause();
- snd.onended();
- snd.currentTime = 0;
- }
- function globalVolume(v){ globalVolume = Math.pow(v,2);}
- function globalStop(){liveSounds.forEach(function(snd){killSound(snd)});}
- function globalPause(){ liveSounds.forEach(function(snd){snd.pause();}); }
- function globalGetLive(){return liveSounds.slice();}
- //Applies a callback to anything that passes the filter
- function instanceApply(name, callback){
- var match = sounds[name].src;
- liveSounds.forEach(function(snd){
- if( snd.src !== match ) return;
- callback(snd);
- });
- }
- SB.volume = function(name, v){
- if(!name)return globalVolume(v);
- instanceVolume[name] = Math.pow(v,2);
- };
- SB.stop = SB.kill = function( name ){
- if(!name)return globalStop();
- instanceApply(name, killSound);
- };
- SB.pause = function( name ){
- if(!name)return globalPause();
- instanceApply(name, function(snd){ snd.pause(); });
- }
- SB.getLive = function( name ){
- if(!name)return globalGetLive();
- return liveSounds.filter(function(snd){ return( snd.src === match );});
- }
- })(this.soundBatch);
- var prototypePlay = Audio.prototype.play;//keep a reference
- // Getting sounds
- this.sound = function(name){
- var snd = new Audio( sounds[name].src ),
- batch = this.sBatch;
- snd.volume = instanceVolume[name];
- snd.play = function(){
- this.volume *= globalVolume;
- prototypePlay.apply(this);
- liveSounds.push(this);
- this.onended = function(){ liveSounds.splice( liveSounds.indexOf(snd), 1 );};
- };
- return snd;
- };
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement