PageSourceSearch

https://archchinese.com/js/mp3.js

js archchinese.com collected 2026-09-24 08:59:06 UTC 23,427 bytes, 744 lines download raw bytes

1(function() {
2  var factory,
3    __hasProp = {}.hasOwnProperty;
4
5  window.WebAudioPlayer = (function() {
6    WebAudioPlayer.getInstance = function() {
7      return this.instance || (this.instance = new this);
8    };
9
10    WebAudioPlayer.prototype.loadedAudio = {};
11
12    WebAudioPlayer.prototype.loadingAudio = {};
13
14    WebAudioPlayer.prototype.playingAudio = {};
15
16    WebAudioPlayer.prototype.usabilityElm = null;
17
18    WebAudioPlayer.prototype.unlockedIOS = false;
19
20    function WebAudioPlayer() {
21      this.usabilityElm = document.createElement('audio');
22      this.audioContext = this.getAudioContext();
23    }
24
25    WebAudioPlayer.prototype.isUsable = function(cb) {
26      var _base;
27      if (cb == null) {
28        cb = function() {};
29      }
30      return cb((this.audioContext != null) && (typeof (_base = this.usabilityElm).canPlayType === "function" ? _base.canPlayType('audio/mpeg') : void 0));
31    };
32
33    WebAudioPlayer.prototype.isPlaying = function(url) {
34      return Object.prototype.hasOwnProperty.call(this.playingAudio, url);
35    };
36
37    WebAudioPlayer.prototype.stopAll = function() {
38      var url;
39      for (url in this.loadingAudio) {
40        this.loadingAudio[url].onLoad = [];
41        this.loadingAudio[url].onError = [];
42      }
43      for (url in this.playingAudio) {
44        this.stop(url);
45      }
46      return true;
47    };
48
49    WebAudioPlayer.prototype.stop = function(url) {
50      var soundData;
51      if (url in this.loadingAudio) {
52        this.loadingAudio[url].onLoad = [];
53        this.loadingAudio[url].onError = [];
54      }
55      soundData = this.playingAudio[url];
56      if (!soundData) {
57        return;
58      }
59      clearTimeout(soundData.onFinishTimer);
60      soundData.gainNode.gain.linearRampToValueAtTime(0, this.audioContext.currentTime + 0.3);
61      delete this.playingAudio[url];
62      return typeof soundData.onStop === "function" ? soundData.onStop(url) : void 0;
63    };
64
65    WebAudioPlayer.prototype.play = function(url, options) {
66      var _ref,
67        _this = this;
68      if (options == null) {
69        options = {};
70      }
71      return this.preload(url, {
72        onLoad: function(duration) {
73          var buffer, bufferSource, gainNode;
74          if (_this.playingAudio[url]) {
75            _this.stop(url);
76          }
77          buffer = _this.loadedAudio[url];
78          bufferSource = _this.audioContext.createBufferSource();
79          bufferSource.buffer = buffer;
80          gainNode = (_this.audioContext.createGainNode || _this.audioContext.createGain).call(_this.audioContext);
81          gainNode.gain.linearRampToValueAtTime(1, _this.audioContext.currentTime);
82          gainNode.connect(_this.audioContext.destination);
83          bufferSource.connect(gainNode);
84          (bufferSource.noteOn || bufferSource.start).call(bufferSource, 0);
85          return _this.playingAudio[url] = {
86            onStop: options.onStop,
87            source: bufferSource,
88            gainNode: gainNode,
89            onFinishTimer: setTimeout(function() {
90              delete _this.playingAudio[url];
91              return typeof options.onFinish === "function" ? options.onFinish(url) : void 0;
92            }, duration)
93          };
94        },
95        onError: function() {
96          return typeof options.onError === "function" ? options.onError(url) : void 0;
97        },
98        timeout: (_ref = options.timeout) != null ? _ref : 0
99      });
100    };
101
102    WebAudioPlayer.prototype.destruct = function(url) {
103      if (Object.prototype.hasOwnProperty.call(this.loadedAudio, url)) {
104        delete this.loadedAudio[url];
105        return true;
106      }
107      return false;
108    };
109
110    WebAudioPlayer.prototype.preload = function(url, options) {
111      var method, xhr, _i, _len, _ref,
112        _this = this;
113      if (options == null) {
114        options = {};
115      }
116      if (!url) {
117        return typeof options.onError === "function" ? options.onError() : void 0;
118      }
119      this.unmuteIOS();
120      if (this.loadedAudio[url]) {
121        return typeof options.onLoad === "function" ? options.onLoad(this.loadedAudio[url].duration * 1000) : void 0;
122      }
123      if (this.loadingAudio[url]) {
124        _ref = ['onLoad', 'onError'];
125        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
126          method = _ref[_i];
127          this.loadingAudio[url][method].push(options[method]);
128        }
129      } else {
130        this.loadingAudio[url] = {
131          onLoad: [options.onLoad],
132          onError: [options.onError]
133        };
134        xhr = new XMLHttpRequest();
135        xhr.open('GET', url, true);
136        xhr.responseType = 'arraybuffer';
137        xhr.onload = function() {
138          return _this.audioContext.decodeAudioData(xhr.response, function(buffer) {
139            var cb, _j, _len1, _ref1;
140            _this.loadedAudio[url] = buffer;
141            _ref1 = _this.loadingAudio[url].onLoad;
142            for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
143              cb = _ref1[_j];
144              if (cb != null) {
145                cb(buffer.duration * 1000);
146              }
147            }
148            return delete _this.loadingAudio[url];
149          }, function() {
150            return _this.handleLoadingError(url);
151          });
152        };
153        xhr.onerror = function() {
154          return _this.handleLoadingError(url);
155        };
156        xhr.send();
157        this.loadingAudio[url].xhr = xhr;
158      }
159      if (options.timeout) {
160        return setTimeout((function() {
161          return _this.handleLoadingError(url);
162        }), Number(options.timeout));
163      }
164    };
165
166    WebAudioPlayer.prototype.unmuteIOS = function() {
167      var bufferSource;
168      if (this.unlockedIOS) {
169        return;
170      }
171      bufferSource = this.audioContext.createBufferSource();
172      bufferSource.connect(this.audioContext.destination);
173      (bufferSource.start || bufferSource.noteOn).call(bufferSource, 0);
174      return this.unlockedIOS = true;
175    };
176
177    WebAudioPlayer.prototype.getAudioContext = function() {
178      try {
179        if (!this.constructor.audioContext) {
180          if (typeof AudioContext !== "undefined" && AudioContext !== null) {
181            this.constructor.audioContext = new AudioContext();
182          } else if (typeof webkitAudioContext !== "undefined" && webkitAudioContext !== null) {
183            this.constructor.audioContext = new webkitAudioContext();
184          }
185        }
186        return this.constructor.audioContext;
187      } catch (_error) {}
188    };
189
190    WebAudioPlayer.prototype.handleLoadingError = function(url) {
191      var cb, _i, _len, _ref;
192      if (!(url in this.loadingAudio)) {
193        return;
194      }
195      this.loadingAudio[url].xhr.abort();
196      _ref = this.loadingAudio[url].onError;
197      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
198        cb = _ref[_i];
199        if (cb != null) {
200          cb(url);
201        }
202      }
203      return delete this.loadingAudio[url];
204    };
205
206    return WebAudioPlayer;
207
208  })();
209
210  window.HtmlAudioPlayer = (function() {
211    HtmlAudioPlayer.getInstance = function() {
212      return this.instance || (this.instance = new this);
213    };
214
215    HtmlAudioPlayer.prototype.loadedAudio = {};
216
217    HtmlAudioPlayer.prototype.loadingAudio = {};
218
219    HtmlAudioPlayer.prototype.playingAudio = {};
220
221    HtmlAudioPlayer.prototype.usabilityElm = null;
222
223    HtmlAudioPlayer.prototype.MAX_VOLUME = 1;
224
225    function HtmlAudioPlayer() {
226      this.usabilityElm = document.createElement('audio');
227    }
228
229    HtmlAudioPlayer.prototype.isUsable = function(cb) {
230      var _ref;
231      if (cb == null) {
232        cb = function() {};
233      }
234      return cb(((_ref = this.usabilityElm) != null ? typeof _ref.canPlayType === "function" ? _ref.canPlayType('audio/mpeg') : void 0 : void 0) && navigator.appVersion.indexOf('MSIE') === -1);
235    };
236
237    HtmlAudioPlayer.prototype.isPlaying = function(url) {
238      return Object.prototype.hasOwnProperty.call(this.playingAudio, url);
239    };
240
241    HtmlAudioPlayer.prototype.stopAll = function() {
242      var url;
243      for (url in this.loadingAudio) {
244        this.loadingAudio[url].onLoad = [];
245        this.loadingAudio[url].onError = [];
246      }
247      for (url in this.playingAudio) {
248        this.stop(url);
249      }
250      return true;
251    };
252
253    HtmlAudioPlayer.prototype.stop = function(url) {
254      var elm, lowerVol, soundData, volume,
255        _this = this;
256      if (url in this.loadingAudio) {
257        this.loadingAudio[url].onLoad = [];
258        this.loadingAudio[url].onError = [];
259      }
260      soundData = this.playingAudio[url];
261      if (!soundData) {
262        return;
263      }
264      clearTimeout(soundData.onFinishTimer);
265      elm = soundData.elm;
266      volume = this.MAX_VOLUME;
267      lowerVol = function() {
268        if (volume > 0) {
269          volume -= 0.03;
270          elm.volume = Math.max(volume, 0);
271          return setTimeout(lowerVol, 10);
272        } else {
273          elm.pause();
274          elm.currentTime = 0;
275          return elm.volume = _this.MAX_VOLUME;
276        }
277      };
278      lowerVol();
279      delete this.playingAudio[url];
280      return typeof soundData.onStop === "function" ? soundData.onStop(url) : void 0;
281    };
282
283    HtmlAudioPlayer.prototype.play = function(url, options) {
284      var _ref,
285        _this = this;
286      if (options == null) {
287        options = {};
288      }
289      return this.preload(url, {
290        onLoad: function(duration) {
291          var elm;
292          if (_this.playingAudio[url]) {
293            _this.stop(url);
294          }
295          elm = _this.loadedAudio[url];
296          elm.play();
297          return _this.playingAudio[url] = {
298            elm: elm,
299            onStop: options.onStop,
300            onFinishTimer: setTimeout(function() {
301              delete _this.playingAudio[url];
302              return typeof options.onFinish === "function" ? options.onFinish(url) : void 0;
303            }, duration)
304          };
305        },
306        onError: function() {
307          return typeof options.onError === "function" ? options.onError(url) : void 0;
308        },
309        timeout: (_ref = options.timeout) != null ? _ref : 0
310      });
311    };
312
313    HtmlAudioPlayer.prototype.destruct = function(url) {
314      if (Object.prototype.hasOwnProperty.call(this.loadedAudio, url)) {
315        delete this.loadedAudio[url];
316        return true;
317      }
318      return false;
319    };
320
321    HtmlAudioPlayer.prototype.preload = function(url, options) {
322      var elm, method, _i, _len, _ref,
323        _this = this;
324      if (options == null) {
325        options = {};
326      }
327      if (!url) {
328        return typeof options.onError === "function" ? options.onError() : void 0;
329      }
330      if (this.loadedAudio[url]) {
331        return typeof options.onLoad === "function" ? options.onLoad(this.loadedAudio[url].duration * 1000) : void 0;
332      }
333      if (this.loadingAudio[url]) {
334        _ref = ['onLoad', 'onError'];
335        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
336          method = _ref[_i];
337          this.loadingAudio[url][method].push(options[method]);
338        }
339      } else {
340        this.loadingAudio[url] = {
341          onLoad: [options.onLoad],
342          onError: [options.onError]
343        };
344        elm = document.createElement('audio');
345        elm.setAttribute('preload', 'auto');
346        elm.addEventListener('loadeddata', function() {
347          var cb, _j, _len1, _ref1;
348          if (!(url in _this.loadingAudio)) {
349            return;
350          }
351          _this.loadedAudio[url] = _this.loadingAudio[url].elm;
352          _ref1 = _this.loadingAudio[url].onLoad;
353          for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
354            cb = _ref1[_j];
355            if (cb != null) {
356              cb(_this.loadedAudio[url].duration * 1000);
357            }
358          }
359          return delete _this.loadingAudio[url];
360        }, false);
361        elm.addEventListener('error', function() {
362          return _this.handleLoadingError(url);
363        }, false);
364        elm.src = url;
365        elm.load();
366        this.loadingAudio[url].elm = elm;
367      }
368      if (options.timeout) {
369        return setTimeout((function() {
370          return _this.handleLoadingError(url);
371        }), Number(options.timeout));
372      }
373    };
374
375    HtmlAudioPlayer.prototype.handleLoadingError = function(url) {
376      var cb, _i, _len, _ref;
377      if (!(url in this.loadingAudio)) {
378        return;
379      }
380      this.loadingAudio[url].elm = null;
381      _ref = this.loadingAudio[url].onError;
382      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
383        cb = _ref[_i];
384        if (cb != null) {
385          cb(url);
386        }
387      }
388      return delete this.loadingAudio[url];
389    };
390
391    return HtmlAudioPlayer;
392
393  })();
394
395  window.FlashAudioPlayer = (function() {
396    FlashAudioPlayer.getInstance = function() {
397      return this.instance || (this.instance = new this);
398    };
399
400    FlashAudioPlayer.prototype.onIsUsable = function() {};
401
402    FlashAudioPlayer.prototype.loadedAudio = {};
403
404    FlashAudioPlayer.prototype.loadingAudio = {};
405
406    FlashAudioPlayer.prototype.playingAudio = {};
407
408    FlashAudioPlayer.prototype.SWF_PATH = '';
409
410    FlashAudioPlayer.prototype.CONTAINER_ID = 'flashAudioContainer';
411
412    FlashAudioPlayer.prototype.FLASH_ID = 'flashAudioObject';
413
414    FlashAudioPlayer.prototype.FLASH_VERSION = [9, 0];
415
416    FlashAudioPlayer.prototype.MAX_VOLUME = 1;
417
418    function FlashAudioPlayer() {
419      this.appendFlashObject();
420    }
421
422    FlashAudioPlayer.prototype.isUsable = function(onIsUsable) {
423      if (onIsUsable == null) {
424        onIsUsable = function() {};
425      }
426      if (this.flashPlugin != null) {
427        return onIsUsable(true);
428      }
429      return this.onIsUsable = onIsUsable;
430    };
431
432    FlashAudioPlayer.prototype.isPlaying = function(url) {
433      return Object.prototype.hasOwnProperty.call(this.playingAudio, url);
434    };
435
436    FlashAudioPlayer.prototype.stopAll = function() {
437      var url;
438      for (url in this.loadingAudio) {
439        this.loadingAudio[url] = {
440          onLoad: [],
441          onError: []
442        };
443      }
444      for (url in this.playingAudio) {
445        this.stop(url);
446      }
447      return true;
448    };
449
450    FlashAudioPlayer.prototype.stop = function(url) {
451      var lowerVol, soundData, volume,
452        _this = this;
453      if (url in this.loadingAudio) {
454        this.loadingAudio[url] = {
455          onLoad: [],
456          onError: []
457        };
458      }
459      soundData = this.playingAudio[url];
460      if (!soundData) {
461        return;
462      }
463      clearTimeout(soundData.onFinishTimer);
464      volume = this.MAX_VOLUME;
465      lowerVol = function() {
466        if (volume > 0) {
467          _this.flashPlugin._setVolume(url, volume -= 0.03);
468          return setTimeout(lowerVol, 10);
469        } else {
470          _this.flashPlugin._stop(url);
471          return _this.flashPlugin._setVolume(url, _this.MAX_VOLUME);
472        }
473      };
474      lowerVol();
475      delete this.playingAudio[url];
476      return typeof soundData.onStop === "function" ? soundData.onStop(url) : void 0;
477    };
478
479    FlashAudioPlayer.prototype.play = function(url, options) {
480      var _ref,
481        _this = this;
482      if (options == null) {
483        options = {};
484      }
485      return this.preload(url, {
486        onLoad: function(duration) {
487          if (_this.playingAudio[url]) {
488            _this.stop(url);
489          }
490          _this.flashPlugin._play(url);
491          return _this.playingAudio[url] = {
492            onStop: options.onStop,
493            onFinishTimer: setTimeout(function() {
494              delete _this.playingAudio[url];
495              return typeof options.onFinish === "function" ? options.onFinish(url) : void 0;
496            }, duration)
497          };
498        },
499        onError: function() {
500          return typeof options.onError === "function" ? options.onError(url) : void 0;
501        },
502        timeout: (_ref = options.timeout) != null ? _ref : 0
503      });
504    };
505
506    FlashAudioPlayer.prototype.destruct = function(url) {
507      if (Object.prototype.hasOwnProperty.call(this.loadedAudio, url)) {
508        delete this.loadedAudio[url];
509        this.flashPlugin._destruct(url);
510        return true;
511      }
512      return false;
513    };
514
515    FlashAudioPlayer.prototype.preload = function(url, options) {
516      var method, _i, _len, _ref,
517        _this = this;
518      if (options == null) {
519        options = {};
520      }
521      if (!url) {
522        return typeof options.onError === "function" ? options.onError() : void 0;
523      }
524      if (this.loadedAudio[url]) {
525        return typeof options.onLoad === "function" ? options.onLoad(this.loadedAudio[url]) : void 0;
526      }
527      if (this.loadingAudio[url]) {
528        _ref = ['onLoad', 'onError'];
529        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
530          method = _ref[_i];
531          this.loadingAudio[url][method].push(options[method]);
532        }
533      } else {
534        this.loadingAudio[url] = {
535          onLoad: [options.onLoad],
536          onError: [options.onError]
537        };
538        this.flashPlugin._preload(url);
539      }
540      if (options.timeout) {
541        return setTimeout((function() {
542          return _this.loadError({
543            url: url
544          });
545        }), Number(options.timeout));
546      }
547    };
548
549    FlashAudioPlayer.prototype.appendFlashObject = function() {
550      var replacement, wrapper,
551        _this = this;
552      wrapper = document.createElement('div');
553      wrapper.id = this.CONTAINER_ID;
554      wrapper.style.position = 'absolute';
555      wrapper.style.marginLeft = '-1px';
556      replacement = document.createElement('div');
557      replacement.id = this.FLASH_ID;
558      wrapper.appendChild(replacement);
559      document.body.appendChild(wrapper);
560      return flashembed(this.FLASH_ID, {
561        src: this.SWF_PATH,
562        width: '1',
563        height: '1',
564        version: this.FLASH_VERSION,
565        onEmbed: function(success, ref) {
566          var api, waitForFlash;
567          if (!(success && ref.getApi())) {
568            return _this.onIsUsable(false);
569          }
570          api = ref.getApi();
571          waitForFlash = function(tries) {
572            if (tries == null) {
573              tries = 5;
574            }
575            if (!tries) {
576              return _this.onIsUsable(false);
577            }
578            return setTimeout(function() {
579              var hasFn, pollFlashObject;
580              hasFn = Object.prototype.hasOwnProperty.call(api, 'PercentLoaded') || (api.PercentLoaded != null);
581              if (hasFn && api.PercentLoaded()) {
582                return pollFlashObject = setInterval(function() {
583                  if (api.PercentLoaded() === 100) {
584                    _this.flashPlugin = api;
585                    _this.onIsUsable(true);
586                    return intervalClear(pollFlashObject);
587                  }
588                }, 250);
589              } else {
590                return waitForFlash(--tries);
591              }
592            }, 100);
593          };
594          return waitForFlash();
595        }
596      });
597    };
598
599    FlashAudioPlayer.prototype.loadError = function(e) {
600      var cb, _i, _len, _ref;
601      if (!(e.url in this.loadingAudio)) {
602        return;
603      }
604      _ref = this.loadingAudio[e.url].onError;
605      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
606        cb = _ref[_i];
607        if (cb != null) {
608          cb(e.url);
609        }
610      }
611      delete this.loadingAudio[e.url];
612      return this.flashPlugin._destruct(e.url);
613    };
614
615    FlashAudioPlayer.prototype.loadComplete = function(e) {
616      var cb, _i, _len, _ref;
617      if (!(e.url in this.loadingAudio)) {
618        return;
619      }
620      this.loadedAudio[e.url] = e.duration;
621      _ref = this.loadingAudio[e.url].onLoad;
622      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
623        cb = _ref[_i];
624        if (cb != null) {
625          cb(e.duration);
626        }
627      }
628      return delete this.loadingAudio[e.url];
629    };
630
631    return FlashAudioPlayer;
632
633  })();
634
635  factory = function() {
636    var AudioPlayer;
637    return AudioPlayer = (function() {
638      AudioPlayer.usablePlugin = null;
639
640      function AudioPlayer(settings) {
641        var method, _fn, _ref,
642          _this = this;
643        if (settings == null) {
644          settings = {};
645        }
646        this.settings = {
647          plugins: settings.plugins || [WebAudioPlayer, HtmlAudioPlayer, FlashAudioPlayer],
648          onUsable: settings.onUsable || function() {},
649          onNotUsable: settings.onNotUsable || function() {
650            var _ref;
651            return (_ref = window.console) != null ? typeof _ref.error === "function" ? _ref.error('Cannot play audio') : void 0 : void 0;
652          }
653        };
654        this.methodBuffer = [];
655        this.plugin = {
656          preload: function(url, options) {
657            if (options == null) {
658              options = {};
659            }
660            return _this.methodBuffer.push(['preload', arguments]);
661          },
662          play: function(url, options) {
663            if (options == null) {
664              options = {};
665            }
666            return _this.methodBuffer.push(['play', arguments]);
667          },
668          isPlaying: function(url) {
669            return _this.methodBuffer.push(['isPlaying', arguments]);
670          },
671          destruct: function(url) {
672            return _this.methodBuffer.push(['destruct', arguments]);
673          },
674          stop: function() {
675            return _this.methodBuffer.push(['stop', arguments]);
676          },
677          stopAll: function() {
678            return _this.methodBuffer.push(['stopAll', arguments]);
679          }
680        };
681        _ref = this.plugin;
682        _fn = function(method) {
683          return _this[method] = function() {
684            return this.plugin[method].apply(this.plugin, arguments);
685          };
686        };
687        for (method in _ref) {
688          if (!__hasProp.call(_ref, method)) continue;
689          _fn(method);
690        }
691        this.initUsablePlugin();
692      }
693
694      AudioPlayer.prototype.initUsablePlugin = function() {
695        var plugin, _base,
696          _this = this;
697        if (this.constructor.usablePlugin != null) {
698          this._initPlugin(this.constructor.usablePlugin);
699          return;
700        }
701        plugin = this.settings.plugins.shift();
702        if (!plugin) {
703          return typeof (_base = this.settings).onNotUsable === "function" ? _base.onNotUsable() : void 0;
704        }
705        return plugin.getInstance().isUsable(function(usable) {
706          if (usable) {
707            return _this._initPlugin(plugin);
708          } else {
709            return _this.initUsablePlugin();
710          }
711        });
712      };
713
714      AudioPlayer.prototype._initPlugin = function(plugin) {
715        var args, method, _base, _ref, _results;
716        this.plugin = plugin.getInstance();
717        this.constructor.usablePlugin = plugin;
718        if (typeof (_base = this.settings).onUsable === "function") {
719          _base.onUsable();
720        }
721        _results = [];
722        while (this.methodBuffer.length) {
723          _ref = this.methodBuffer.shift(), method = _ref[0], args = _ref[1];
724          _results.push(this.plugin[method].apply(this.plugin, args));
725        }
726        return _results;
727      };
728
729      return AudioPlayer;
730
731    })();
732  };
733
734  (function(root, factory) {
735    if (typeof define === 'function' && define.amd) {
736      return define([], factory);
737    } else if ((typeof module !== "undefined" && module !== null ? module.exports : void 0) != null) {
738      return module.exports = factory();
739    } else {
740      return root.AudioPlayer = factory();
741    }
742  })(this, factory);
743
744}).call(this);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.