-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathboombox.js
3144 lines (2695 loc) · 89.6 KB
/
boombox.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Browser sound library which blended HTMLVideo and HTMLAudio and WebAudio
*
* The MIT License (MIT)
*
* Copyright (c) 2014-2015 CyberAgent, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*
* @license MIT
* @author Kei Funagayama <[email protected]>
*/
(function (w) {
'use strict';
var none = function () {};
var isRequire = !!(typeof define === 'function' && define.amd);
var SPRITE_SEPARATOR = '-';
var LOGGER_DEFAULT_SEPARATOR = '-';
var getSpriteName = function (name) {
return {
prefix: name.substring(0, name.indexOf(SPRITE_SEPARATOR)),
suffix: name.substring(name.indexOf(SPRITE_SEPARATOR) + 1),
name: name
};
};
/**
* trace: 1
* debug: 2
* info: 3
* warn: 4
* error: 5
*
*/
var LOG_LEVEL = 3; // default: info
var Logger = (function () {
var ArrayProto = Array.prototype;
var slice = ArrayProto.slice;
function Logger(prefix) {
this.prefix = prefix || LOGGER_DEFAULT_SEPARATOR;
this.prefix = '[' + this.prefix + ']';
}
/**
* Log output (trace)
* @memberof Logger
* @name trace
*/
Logger.prototype.trace = function () {
if (LOG_LEVEL <= 1) {
if (!w.console) {
} else if (w.console.trace) {
w.console.trace('[TRACE]', this.prefix, slice.call(arguments).join(' '));
} else if (w.console.debug) {
w.console.debug('[TRACE]', this.prefix, slice.call(arguments).join(' '));
} else {
w.console.log('[TRACE]', this.prefix, slice.call(arguments).join(' '));
}
}
};
/**
* Log output (debug)
* @memberof Logger
* @name debug
*/
Logger.prototype.debug = function () {
if (LOG_LEVEL <= 2) {
if (!w.console) {
} else if (w.console.debug) {
w.console.debug('[DEBUG]', this.prefix, slice.call(arguments).join(' '));
} else {
w.console.log('[DEBUG]', this.prefix, slice.call(arguments).join(' '));
}
}
};
/**
* Log output (info)
* @memberof Logger
* @name info
*/
Logger.prototype.info = function () {
if (LOG_LEVEL <= 3) {
w.console && w.console.info('[INFO]', this.prefix, slice.call(arguments).join(' '));
}
};
/**
* Log output (warn)
* @memberof Logger
* @name warn
*/
Logger.prototype.warn = function () {
if (LOG_LEVEL <= 4) {
w.console && w.console.warn('[WARN]', this.prefix, slice.call(arguments).join(' '));
}
};
/**
* Log output (error)
* @memberof Logger
* @name error
*/
Logger.prototype.error = function () {
if (LOG_LEVEL <= 5) {
w.console && w.console.error('[ERROR]', this.prefix, slice.call(arguments).join(' '));
}
};
return Logger;
})();
//////////////////////////////////
// Boombox Class
var Boombox = (function () {
function Boombox() {
/**
* Version
* @memberof Boombox
* @name VERSION
*/
this.VERSION = '1.0.9';
/**
* Loop off
*
* @memberof Boombox
* @name LOOP_NOT
* @constant
* @type {Interger}
*/
this.LOOP_NOT = 0;
/**
* orignal loop
*
* @memberof Boombox
* @name LOOP_ORIGINAL
* @constant
* @type {Interger}
*/
this.LOOP_ORIGINAL = 1;
/**
* Native loop
*
* @memberof Boombox
* @name LOOP_NATIVE
* @constant
* @type {Interger}
*/
this.LOOP_NATIVE = 2;
/**
* Turn off the power.
*
* @memberof Boombox
* @name POWER_OFF
* @constant
* @type {Boolean}
*/
this.POWER_OFF = false;
/**
* Turn on the power.
*
* @memberof Boombox
* @name POWER_ON
* @constant
* @type {Boolean}
*/
this.POWER_ON = true;
/**
* It does not support the media type.
*
* @memberof Boombox
* @name ERROR_MEDIA_TYPE
* @constant
* @type {Interger}
*/
this.ERROR_MEDIA_TYPE = 0;
/**
* Hit the filter
*
* @memberof Boombox
* @name ERROR_HIT_FILTER
* @constant
* @type {Interger}
*/
this.ERROR_HIT_FILTER = 1;
/**
* Threshold to determine whether sound source is finished or not
*
* @memberof Boombox
* @name THRESHOLD
* @type {Interger}
*/
this.THRESHOLD = 0.2;
/**
* flag setup
* @memberof Boombox
* @name setuped
* @type {Boolean}
*/
this.setuped = false;
/**
* AudioContext
* @memberof Boombox
* @name AudioContext
* @type {AudioContext}
*/
this.AudioContext = w.AudioContext || w.webkitAudioContext;
/**
* Environmental support information
*
* @memberof Boombox
* @name support
* @type {Object}
*/
this.support = {
mimes: [],
webaudio: {
use: !!this.AudioContext
},
htmlaudio: {
use: false
},
htmlvideo: {
use: false
}
};
if (this.support.webaudio.use) {
/**
* WebAudioContext instance (singleton)
*
* @memberof Boombox
* @name WEB_AUDIO_CONTEXT
* @type {AudioContext}
*/
this.WEB_AUDIO_CONTEXT = new this.AudioContext();
if (!this.WEB_AUDIO_CONTEXT.createGain) {
this.WEB_AUDIO_CONTEXT.createGain = this.WEB_AUDIO_CONTEXT.createGainNode;
}
}
// Check HTML Audio support.
try {
/**
* Test local HTMLAudio
* @memberof Boombox
* @name _audio
* @type {HTMLAudioElement}
*/
if (new w.Audio().canPlayType) {
this.support.htmlaudio.use = true;
} else {
this.support.htmlaudio.use = false;
}
} catch (e) {
this.support.htmlaudio.use = false;
}
// Check HTML Video support.
try {
/**
* Test local HTMLVideo
* @memberof Boombox
* @name _video
* @type {HTMLVideoElement}
*/
if (document.createElement('video').canPlayType) {
this.support.htmlvideo.use = true;
} else {
this.support.htmlvideo.use = false;
}
} catch (e) {
this.support.htmlvideo.use = false;
}
/**
* Audio instance pool
*
* @memberof Boombox
* @name pool
*/
this.pool = {};
/**
* Audio instance of waiting
*
* @memberof Boombox
* @name waits
*/
this.waits = [];
/**
* Visibility of browser
*
* @memberof Boombox
* @name visibility
*/
this.visibility = {
hidden: undefined,
visibilityChange: undefined
};
/**
* State of boombox
*
* @memberof Boombox
* @name state
* @type {Object}
*/
this.state = {
power: this.POWER_ON
};
/**
* Filtering function
*
* @memberof Boombox
* @name filter
* @type {Object}
*/
this.filter = {};
}
//// prototype
/**
* The availability of the WebLAudio
*
* @memberof Boombox
* @name isWebAudio
* @return {Boolean}
*/
Boombox.prototype.isWebAudio = function () {
return this.support.webaudio.use;
};
/**
* The availability of the HTMLAudio
*
* @memberof Boombox
* @name isHTMLAudio
* @return {Boolean}
*/
Boombox.prototype.isHTMLAudio = function () {
return this.support.htmlaudio.use;
};
/**
* The availability of the HTMLVideo
*
* @memberof Boombox
* @name isHTMLVideo
* @return {Boolean}
*/
Boombox.prototype.isHTMLVideo = function () {
return this.support.htmlvideo.use;
};
/**
* boombox to manage, Audio is playing
*
* @memberof Boombox
* @name isPlayback
* @return {Boolean}
*/
Boombox.prototype.isPlayback = function () {
var self = this;
var res = false;
for (var name in this.pool) {
if (this.pool[name].isPlayback()) {
res = true;
break;
}
}
return res;
};
/**
* Setup processing
*
* @memberof Boombox
* @name setup
* @param {Object} options
* @return {Boombox}
* @example
* var options = {
* webaudio: {use: Boolean},
* htmlaudio: {use: Boolean},
* htmlvideo: {use: Boolean},
* loglevel: Number, ) trace:1, debug:2, info:3, warn:4, error:5
* }
*
*/
Boombox.prototype.setup = function setup(options) {
var self = this;
options = options || {};
if (typeof options.threshold !== 'undefined') {
this.THRESHOLD = options.threshold;
}
if (typeof options.loglevel !== 'undefined') {
LOG_LEVEL = options.loglevel;
}
this.logger = new Logger('Boombox '); // log
if (this.setuped) {
this.logger.warn('"setup" already, are running.');
return this;
}
if (options.webaudio) {
if (typeof options.webaudio.use !== 'undefined') {
this.support.webaudio.use = options.webaudio.use;
this.logger.info('options.webaudio.use:', this.support.webaudio.use);
}
}
if (options.htmlaudio) {
if (typeof options.htmlaudio.use !== 'undefined') {
this.support.htmlaudio.use = options.htmlaudio.use;
this.logger.info('options.htmlaudio.use: ', this.support.htmlaudio.use);
}
}
if (options.htmlvideo) {
if (typeof options.htmlvideo.use !== 'undefined') {
this.support.htmlvideo.use = options.htmlvideo.use;
this.logger.info('options htmlvideo', this.support.htmlvideo);
}
}
// scan browser
this._browserControl();
// Log: WebAudio support.
if (this.support.webaudio.use) {
this.logger.debug('WebAudio use support.');
} else {
this.logger.debug('WebAudio use not support.');
}
// Log: HTMLAudio support.
if (this.support.htmlaudio.use) {
this.logger.debug('HTMLAudio use support.');
} else {
this.logger.debug('HTMLAudio use not support.');
}
// Log: HTMLVideo support.
if (this.support.htmlvideo.use) {
this.logger.debug('HTMLVideo use support.');
} else {
this.logger.debug('HTMLVideo use not support.');
}
//this.logger.debug('support:', JSON.stringify(this.support));
this.setuped = true;
return this;
};
/**
* Get Audio instance
*
* @memberof Boombox
* @name get
* @param {String} name audio name
* @return {WebAudio|HTMLAudio|HTMLVideo}
*/
Boombox.prototype.get = function (name) {
return this.pool[name];
};
/**
* Loading audio
*
* @memberof Boombox
* @name load
* @param {String} name audio name
* @param {Object} options Audio options
* @param {Boolean} useHTMLVideo forced use HTMLVideo
* @param {Function} callback
* @return {Boombox}
* @example
* var options = {
* src: [
* {
* media: 'audio/mp4',
* path: 'http://example.com/sample.m4a',
* }
* ]
* filter: ['android2', 'android4', 'ios5', 'ios6', 'ios7' ]
* }
*
*/
Boombox.prototype.load = function (name, options, useHTMLVideo, callback) {
if (typeof arguments[2] === 'function') {
callback = useHTMLVideo;
useHTMLVideo = null;
}
if (!this.setuped) {
callback && callback(new Error('setup incomplete boombox. run: boombox.setup(options)'));
return this;
}
if (this.pool[name]) {
this.logger.trace('audio pool cache hit!!', name);
return callback && callback(null, this.pool[name]);
}
// check support media type
var src = this.useMediaType(options.src);
if (src) {
options.media = src.media;
options.src = src.path;
}
// forced use HTMLVideo
if (useHTMLVideo && this.isHTMLVideo()) {
var htmlvideo = new boombox.HTMLVideo(name);
if (!src) {
htmlvideo.state.error = this.ERROR_MEDIA_TYPE;
}
if (typeof htmlvideo.state.error === 'undefined' && !this.runfilter(htmlvideo, options)) {
htmlvideo.load(options, callback);
} else {
return callback && callback(new Error(htmlvideo.state.error), htmlvideo);
}
this.setPool(name, htmlvideo, boombox.HTMLVideo);
return this;
}
//////////////////////
if (this.isWebAudio()) {
this.logger.debug("use web audio");
var webaudio = new boombox.WebAudio(name);
if (!src) {
webaudio.state.error = this.ERROR_MEDIA_TYPE;
}
if (typeof webaudio.state.error === 'undefined' && !this.runfilter(webaudio, options)) {
webaudio.load(options, callback);
} else {
return callback && callback(new Error(webaudio.state.error), webaudio);
}
this.setPool(name, webaudio, boombox.WebAudio);
} else if (this.isHTMLAudio()) {
this.logger.debug("use html audio");
var htmlaudio = new boombox.HTMLAudio(name);
if (!src) {
htmlaudio.state.error = this.ERROR_MEDIA_TYPE;
}
if (typeof htmlaudio.state.error === 'undefined' && !this.runfilter(htmlaudio, options)) {
htmlaudio.load(options, callback);
} else {
return callback && callback(new Error(htmlaudio.state.error), htmlaudio);
}
this.setPool(name, htmlaudio, boombox.HTMLAudio);
} else {
callback && callback(new Error('This environment does not support HTMLAudio, both WebAudio both.'), this);
}
return this;
};
/**
* remove audio
*
* @memberof Boombox
* @name remove
* @param {String} name
* @return {Boombox}
*/
Boombox.prototype.remove = function (name) {
if (this.pool[name]) { // change object
this.logger.trace('Remove Audio that is pooled. name', name);
this.pool[name].dispose && this.pool[name].dispose();
this.pool[name] = undefined;
delete this.pool[name];
}
return this;
};
/**
* Set pool
*
* @memberof Boombox
* @name setPool
* @param {String} name
* @param {AudioContext|HTMLAudioElement|HTMLVideoElement} obj Browser audio instance
* @param {WebAudio|HTMLAudio|HTMLVideo} Obj Boombox audio class
* @return {Boombox}
*/
Boombox.prototype.setPool = function (name, obj, Obj) {
if (obj.isParentSprite()) {
for (var r in this.pool) {
if (!!~r.indexOf(name + SPRITE_SEPARATOR)) {
delete this.pool[r];
}
}
for (var n in obj.sprite.options) {
var cname = name + SPRITE_SEPARATOR + n;
var audio = new Obj(cname, obj);
this.pool[audio.name] = audio;
}
}
this.remove(name);
this.pool[name] = obj;
return this;
};
/**
* Run filter
*
* @memberof Boombox
* @name runfilter
* @param {WebAudio|HTMLAudio|HTMLVideo} audio Boombox audio instance
* @param {Object} options
* @return {Boolean}
*/
Boombox.prototype.runfilter = function (audio, options) {
var hit;
var list = options.filter || [];
for (var i = 0; i < list.length; i++) {
var name = list[i];
var fn = this.filter[name];
if (!fn) {
this.logger.warn('filter not found. name:', name);
return;
}
this.logger.debug('filter run. name:', name);
if (fn(name, audio, options)) {
hit = name;
break;
}
}
if (hit) {
audio.state.error = this.ERROR_HIT_FILTER; // set error
this.logger.warn('Hit the filter of', hit);
return true;
}
return false;
};
/**
* check support media type
*
* @memberof Boombox
* @name useMediaType
* @param {Array} src audio file data
* @return {Object|undefined}
*/
Boombox.prototype.useMediaType = function (src) {
for (var i = 0; i < src.length; i++) {
var t = src[i];
if (new w.Audio().canPlayType(t.media)) {
return t;
} else {
this.logger.warn('skip audio type.', t.media);
}
}
return undefined;
};
/**
* pause sound playback in managing boombox
*
* @memberof Boombox
* @name pause
* @return {boombox}
*/
Boombox.prototype.pause = function () {
var self = this;
this.logger.trace('pause');
for (var name in this.pool) {
var audio = this.pool[name];
audio.pause();
self.waits.push(name);
}
return this;
};
/**
* resume the paused, to manage the boombox
*
* @memberof Boombox
* @name resume
* @return {Boombox}
*/
Boombox.prototype.resume = function () {
this.logger.trace('resume');
var name = this.waits.shift();
if (name && this.pool[name]) {
this.pool[name].resume();
}
if (0 < this.waits.length) {
this.resume();
}
return this;
};
/**
* Change all audio power on/off
*
* @memberof Boombox
* @name power
* @param {Boolean} p power on/off. boombox.(POWER_ON|POWER_OFF)
* @return {Boombox}
*/
Boombox.prototype.power = function (p) {
var self = this;
this.logger.trace('power:', this.name, 'flag:', p);
for (var name in this.pool) {
var audio = this.pool[name];
audio.power(p);
}
this.state.power = p;
return this;
};
/**
* audio change volume.
*
* @memberof Boombox
* @method
* @name volume
* @param {Interger} v volume
* @return {Boombox}
*/
Boombox.prototype.volume = function (v) {
var self = this;
this.logger.trace('volume:', this.name, 'volume:', v);
for (var name in this.pool) {
var audio = this.pool[name];
audio.volume(v);
}
return this;
};
/**
* Firing in the occurrence of events VisibilityChange
*
* @memberof Boombox
* @name onVisibilityChange
* @param {Event} e event
*/
Boombox.prototype.onVisibilityChange = function (e) {
this.logger.trace('onVisibilityChange');
if (document[this.visibility.hidden]) {
this.pause();
} else {
this.resume();
}
};
/**
* Firing in the occurrence of events window.onfocus
*
* @memberof Boombox
* @name onFocus
* @param {Event} e event
*/
Boombox.prototype.onFocus = function (e) {
this.logger.trace('onFocus');
this.resume();
};
/**
* Firing in the occurrence of events window.onblur
*
* @memberof Boombox
* @name onBlur
* @param {Event} e event
*/
Boombox.prototype.onBlur = function (e) {
this.logger.trace('onBlur');
this.pause();
};
/**
* Firing in the occurrence of events window.onpageshow
*
* @memberof Boombox
* @name onPageShow
* @param {Event} e event
*/
Boombox.prototype.onPageShow = function (e) {
this.logger.trace('onPageShow');
this.resume();
};
/**
* Firing in the occurrence of events window.onpagehide
*
* @memberof Boombox
* @name onPageHide
* @param {Event} e event
*/
Boombox.prototype.onPageHide = function (e) {
this.logger.trace('onPageHide');
this.pause();
};
/**
* Scan browser differences
*
* @memberof Boombox
* @name _browserControl
* @return {Boombox}
*/
Boombox.prototype._browserControl = function () {
var self = this;
if (typeof document.hidden !== "undefined") {
this.visibility.hidden = "hidden";
this.visibility.visibilityChange = "visibilitychange";
} else if (typeof document.webkitHidden !== "undefined") {
this.visibility.hidden = "webkitHidden";
this.visibility.visibilityChange = "webkitvisibilitychange";
} else if (typeof document.mozHidden !== "undefined") {
this.visibility.hidden = "mozHidden";
this.visibility.visibilityChange = "mozvisibilitychange";
} else if (typeof document.msHidden !== "undefined") {
this.visibility.hidden = "msHidden";
this.visibility.visibilityChange = "msvisibilitychange";
}
// Visibility.hidden
if (this.visibility.hidden) {
document.addEventListener(this.visibility.visibilityChange, function (e) {
self.onVisibilityChange(e);
}, false);
}
// focus/blur
if (typeof window.addEventListener !== "undefined") {
window.addEventListener('focus', function (e) {
self.onFocus(e);
}, false);
window.addEventListener('blur', function (e) {
self.onBlur(e);
}, false);
} else {
window.attachEvent('onfocusin', function (e) {
self.onFocus(e);
}, false);
window.attachEvent('onfocusout', function (e) {
self.onBlur(e);
}, false);
}
// onpage show/hide
window.onpageshow = function (e) {
self.onPageShow(e);
};
window.onpagehide = function (e) {
self.onPageHide(e);
};
//
return this;
};
/**
* Adding filtering
*
* @memberof Boombox
* @name addFilter
* @param {String} filter name
* @param {Function} filter function
* @return {Boombox}
*/
Boombox.prototype.addFilter = function (name, fn) {
this.filter[name] = fn;
return this;
};
/**
* dispose
*
* @memberof Boombox
* @name dispose
*/
Boombox.prototype.dispose = function () {
for (var name in this.pool) {
var audio = this.pool[name];
audio.dispose && audio.dispose();
}
delete this.VERSION;
delete this.setuped;
delete this.support.mimes;
delete this.support.webaudio.use;
this.WEB_AUDIO_CONTEXT && delete this.WEB_AUDIO_CONTEXT;
delete this.support.webaudio;
delete this.support.htmlaudio.use;
delete this.support.htmlaudio;
delete this.support.htmlvideo.use;
delete this.support.htmlvideo;
delete this.support;
delete this.pool;
delete this.waits;
delete this.visibility;
delete this.state.power;
delete this.state;
delete this.filter;
};
return Boombox;
})();
//////////////////////////////////
// New!!!!
var boombox = new Boombox();
//////////////////////////////////
// HTMLAudio Class