Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. var MediaMaster = new (function(){
  2. var sounds = {};// a container of Audio instances and names(keys) as a reference to their respective values.
  3. // Example: sounds = {"jumpSound":new Audio('path')};
  4. // Master Controll of sounds
  5. var liveSounds = [],
  6. instanceVolume = {},
  7. globalVolume = 1;
  8.  
  9. this.soundBatch = this.sBatch = {muted: false};
  10. // Hide global controls
  11. (function(SB){
  12. function killSound(snd){
  13. snd.pause();
  14. snd.onended();
  15. snd.currentTime = 0;
  16. }
  17. function globalVolume(v){ globalVolume = Math.pow(v,2);}
  18. function globalStop(){liveSounds.forEach(function(snd){killSound(snd)});}
  19. function globalPause(){ liveSounds.forEach(function(snd){snd.pause();}); }
  20. function globalGetLive(){return liveSounds.slice();}
  21. //Applies a callback to anything that passes the filter
  22. function instanceApply(name, callback){
  23. var match = sounds[name].src;
  24. liveSounds.forEach(function(snd){
  25. if( snd.src !== match ) return;
  26. callback(snd);
  27. });
  28. }
  29.  
  30.  
  31. SB.volume = function(name, v){
  32. if(!name)return globalVolume(v);
  33. instanceVolume[name] = Math.pow(v,2);
  34. };
  35. SB.stop = SB.kill = function( name ){
  36. if(!name)return globalStop();
  37. instanceApply(name, killSound);
  38. };
  39. SB.pause = function( name ){
  40. if(!name)return globalPause();
  41. instanceApply(name, function(snd){ snd.pause(); });
  42. }
  43. SB.getLive = function( name ){
  44. if(!name)return globalGetLive();
  45. return liveSounds.filter(function(snd){ return( snd.src === match );});
  46. }
  47. })(this.soundBatch);
  48.  
  49.  
  50.  
  51. var prototypePlay = Audio.prototype.play;//keep a reference
  52.  
  53. // Getting sounds
  54. this.sound = function(name){
  55. var snd = new Audio( sounds[name].src ),
  56. batch = this.sBatch;
  57. snd.volume = instanceVolume[name];
  58. snd.play = function(){
  59. this.volume *= globalVolume;
  60. prototypePlay.apply(this);
  61. liveSounds.push(this);
  62. this.onended = function(){ liveSounds.splice( liveSounds.indexOf(snd), 1 );};
  63. };
  64. return snd;
  65. };
  66. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement