JAVASCRIPT 26
SoundControl.js Guest on 19th April 2021 07:59:17 PM
  1. var SoundControl = function () {
  2.     if (arguments.callee.instance) {
  3.         return arguments.callee.instance;
  4.     }
  5.     arguments.callee.instance = this;
  6.  
  7.     var configuration = ConfigurationLoader.getInstance();
  8.     me = this;
  9.     //Init sounds
  10.     me.registerSound = me.initSound(configuration.registerSound, false, "auto");
  11.     me.messageSound = me.initSound(configuration.messageSound, false, "auto");
  12.     me.ringSound = me.initSound(configuration.ringSound, true, "none");
  13.     me.busySound = me.initSound(configuration.busySound, false, "none");
  14.     me.finishSound = me.initSound(configuration.finishSound, false, "none");
  15. };
  16.  
  17. SoundControl.getInstance = function () {
  18.     return new SoundControl();
  19. };
  20.  
  21. SoundControl.prototype = {
  22.  
  23.     //Creates HTML5 audio tag
  24.     initSound: function (src, loop, preload) {
  25.         if (typeof loop == 'undefined') {
  26.             loop = false;
  27.         }
  28.         var audioTag = document.createElement("audio");
  29.         audioTag.autoplay = false;
  30.         audioTag.preload = preload;
  31.         if (loop) {
  32.             audioTag.loop = true;
  33.         }
  34.         //add src tag to audio tag
  35.         audioTag.src = src;
  36.         document.body.appendChild(audioTag);
  37.         return audioTag;
  38.     },
  39.  
  40.     //plays audio
  41.     playSound: function (soundName) {
  42.         try {
  43.             switch (soundName) {
  44.                 case "RING":
  45.                     if (!ConfigurationLoader.getInstance().disableLocalRing) {
  46.                         me.ringSound.play();
  47.                     }
  48.                     break;
  49.                 case "BUSY":
  50.                     me.busySound.play();
  51.                     break;
  52.                 case "REGISTER":
  53.                     me.registerSound.play();
  54.                     break;
  55.                 case "FINISH":
  56.                     me.finishSound.play();
  57.                     break;
  58.                 case "MESSAGE":
  59.                     me.messageSound.play();
  60.                     break;
  61.                 default:
  62.                     console.error("Do not know what to play on " + soundName);
  63.  
  64.             }
  65.         }
  66.         catch (err) {
  67.             console.info("This browser may not support sound.play() method: " + err)
  68.         }
  69.     },
  70.  
  71.     //stops audio
  72.     stopSound: function (soundName) {
  73.         try {
  74.             switch (soundName) {
  75.                 case "RING":
  76.                     if (me.ringSound && !me.ringSound.paused) {
  77.                         me.ringSound.pause();
  78.                         me.ringSound.currentTime = 0;
  79.                     }
  80.                     break;
  81.                 default:
  82.                     console.error("Do not know what to stop on " + soundName);
  83.  
  84.             }
  85.         } catch (err) {
  86.             console.info("This browser may not support sound.pause() method or sound.currentTime property: " + err)
  87.         }
  88.     }
  89. };

Paste-bin is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.