Advertisement
Guest User

Sound Manager

a guest
Jun 19th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Get IE or Edge browser version
  2. var IEVersion = detectIE();
  3.  
  4. /**
  5.  * detect IE
  6.  * returns version of IE or false, if browser is not Internet Explorer
  7.  */
  8. function detectIE() {
  9.   var ua = window.navigator.userAgent;
  10.  
  11.   // IE 10
  12.   // ua = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)';
  13.  
  14.   // IE 11
  15.   // ua = 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko';
  16.  
  17.   // Edge 12 (Spartan)
  18.   // ua = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0';
  19.  
  20.   // Edge 13
  21.   // ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586';
  22.  
  23.   var msie = ua.indexOf('MSIE ');
  24.   if (msie > 0) {
  25.     // IE 10 or older => return version number
  26.     return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
  27.   }
  28.  
  29.   var trident = ua.indexOf('Trident/');
  30.   if (trident > 0) {
  31.     // IE 11 => return version number
  32.     var rv = ua.indexOf('rv:');
  33.     return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
  34.   }
  35.  
  36.   var edge = ua.indexOf('Edge/');
  37.   if (edge > 0) {
  38.     // Edge (IE 12+) => return version number
  39.     return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
  40.   }
  41.  
  42.   // other browser
  43.   return false;
  44. }
  45.  
  46. if(IEVersion > 12)
  47. var SoundManager = (function () {
  48.  
  49.   window.AudioContext = window.AudioContext || window.webkitAudioContext;
  50.  
  51.     /// custom buffer loader
  52.     /// see http://www.html5rocks.com/en/tutorials/webaudio/intro/
  53.     function BufferLoader(context, urlList, callback) {
  54.         this.context = context;
  55.         this.urlList = urlList;
  56.         this.onload = callback;
  57.         this.bufferList = new Array();
  58.         this.loadCount = 0;
  59.     }
  60.  
  61.     BufferLoader.prototype.loadBuffer = function (url, index) {
  62.         // Load buffer asynchronously
  63.         var request = new XMLHttpRequest();
  64.         request.open("GET", url, true);
  65.         request.responseType = "arraybuffer";
  66.  
  67.         var loader = this;
  68.  
  69.         request.onload = function () {
  70.             // Asynchronously decode the audio file data in request.response
  71.             loader.context.decodeAudioData(
  72.             request.response,
  73.             function (buffer) {
  74.                 if (!buffer) {
  75.                     alert('error decoding file data: ' + url);
  76.                     return;
  77.                 }
  78.                 loader.bufferList[index] = buffer;
  79.                 if (++loader.loadCount == loader.urlList.length) loader.onload(loader.bufferList);
  80.             },
  81.             function (error) {
  82.                 console.error('decodeAudioData error', error);
  83.             });
  84.         }
  85.  
  86.         request.onerror = function (e) {
  87.             alert('BufferLoader: XHR error');
  88.             console.log(e);
  89.         }
  90.  
  91.         request.send();
  92.     }
  93.  
  94.     BufferLoader.prototype.load = function () {
  95.         for (var i = 0; i < this.urlList.length; ++i)
  96.         this.loadBuffer(this.urlList[i], i);
  97.     }
  98.  
  99.     /// setup audio context and start loading samples
  100.     var actx = new AudioContext(),
  101.         blst,
  102.         bLoader = new BufferLoader(
  103.         actx, [
  104.               window.location.origin + '/mobile/sounds/closeToYou.wav',
  105.               window.location.origin + '/mobile/sounds/arrivedAtLocation.wav',
  106.             ],
  107.         done),
  108.         isReady = false;
  109.  
  110.     /// start loading the samples
  111.     bLoader.load();
  112.  
  113.     /// when samples are loaded update status
  114.     function done(bl) {
  115.         blst = bl;
  116.         isReady = true;
  117.         $('#status').html('Ready!');
  118.     }
  119.  
  120.     /// this sets up chain so we can play audio
  121.     function play(i) {
  122.         var src = actx.createBufferSource();
  123.         src.buffer = blst[i];
  124.         src.connect(actx.destination);
  125.         src.start(0);
  126.     }
  127.  
  128.     this.playCloseToMeLocation = function () {
  129.         play(0);
  130.     };
  131.  
  132.     this.playArrivedAtLocation = function () {
  133.         play(1);
  134.     };
  135.  
  136.   return this;
  137. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement