-
Notifications
You must be signed in to change notification settings - Fork 85
/
soundmanager2.js
5718 lines (4071 loc) · 149 KB
/
soundmanager2.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
/** @license
*
* SoundManager 2: JavaScript Sound for the Web
* ----------------------------------------------
* http://schillmania.com/projects/soundmanager2/
*
* Copyright (c) 2007, Scott Schiller. All rights reserved.
* Code provided under the BSD License:
* http://schillmania.com/projects/soundmanager2/license.txt
*
* V2.97a.20130324 ("Mahalo" Edition)
*/
/*global window, SM2_DEFER, sm2Debugger, console, document, navigator, setTimeout, setInterval, clearInterval, Audio, opera */
/*jslint regexp: true, sloppy: true, white: true, nomen: true, plusplus: true, todo: true */
/**
* About this file
* -------------------------------------------------------------------------------------
* This is the fully-commented source version of the SoundManager 2 API,
* recommended for use during development and testing.
*
* See soundmanager2-nodebug-jsmin.js for an optimized build (~11KB with gzip.)
* http://schillmania.com/projects/soundmanager2/doc/getstarted/#basic-inclusion
* Alternately, serve this file with gzip for 75% compression savings (~30KB over HTTP.)
*
* You may notice <d> and </d> comments in this source; these are delimiters for
* debug blocks which are removed in the -nodebug builds, further optimizing code size.
*
* Also, as you may note: Whoa, reliable cross-platform/device audio support is hard! ;)
*/
(function(window, _undefined) {
"use strict";
var soundManager = null;
/**
* The SoundManager constructor.
*
* @constructor
* @param {string} smURL Optional: Path to SWF files
* @param {string} smID Optional: The ID to use for the SWF container element
* @this {SoundManager}
* @return {SoundManager} The new SoundManager instance
*/
function SoundManager(smURL, smID) {
/**
* soundManager configuration options list
* defines top-level configuration properties to be applied to the soundManager instance (eg. soundManager.flashVersion)
* to set these properties, use the setup() method - eg., soundManager.setup({url: '/swf/', flashVersion: 9})
*/
this.setupOptions = {
'url': (smURL || null), // path (directory) where SoundManager 2 SWFs exist, eg., /path/to/swfs/
'flashVersion': 8, // flash build to use (8 or 9.) Some API features require 9.
'debugMode': true, // enable debugging output (console.log() with HTML fallback)
'debugFlash': false, // enable debugging output inside SWF, troubleshoot Flash/browser issues
'useConsole': true, // use console.log() if available (otherwise, writes to #soundmanager-debug element)
'consoleOnly': true, // if console is being used, do not create/write to #soundmanager-debug
'waitForWindowLoad': false, // force SM2 to wait for window.onload() before trying to call soundManager.onload()
'bgColor': '#ffffff', // SWF background color. N/A when wmode = 'transparent'
'useHighPerformance': false, // position:fixed flash movie can help increase js/flash speed, minimize lag
'flashPollingInterval': null, // msec affecting whileplaying/loading callback frequency. If null, default of 50 msec is used.
'html5PollingInterval': null, // msec affecting whileplaying() for HTML5 audio, excluding mobile devices. If null, native HTML5 update events are used.
'flashLoadTimeout': 1000, // msec to wait for flash movie to load before failing (0 = infinity)
'wmode': null, // flash rendering mode - null, 'transparent', or 'opaque' (last two allow z-index to work)
'allowScriptAccess': 'always', // for scripting the SWF (object/embed property), 'always' or 'sameDomain'
'useFlashBlock': false, // *requires flashblock.css, see demos* - allow recovery from flash blockers. Wait indefinitely and apply timeout CSS to SWF, if applicable.
'useHTML5Audio': true, // use HTML5 Audio() where API is supported (most Safari, Chrome versions), Firefox (no MP3/MP4.) Ideally, transparent vs. Flash API where possible.
'html5Test': /^(probably|maybe)$/i, // HTML5 Audio() format support test. Use /^probably$/i; if you want to be more conservative.
'preferFlash': true, // overrides useHTML5audio. if true and flash support present, will try to use flash for MP3/MP4 as needed since HTML5 audio support is still quirky in browsers.
'noSWFCache': false // if true, appends ?ts={date} to break aggressive SWF caching.
};
this.defaultOptions = {
/**
* the default configuration for sound objects made with createSound() and related methods
* eg., volume, auto-load behaviour and so forth
*/
'autoLoad': false, // enable automatic loading (otherwise .load() will be called on demand with .play(), the latter being nicer on bandwidth - if you want to .load yourself, you also can)
'autoPlay': false, // enable playing of file as soon as possible (much faster if "stream" is true)
'from': null, // position to start playback within a sound (msec), default = beginning
'loops': 1, // how many times to repeat the sound (position will wrap around to 0, setPosition() will break out of loop when >0)
'onid3': null, // callback function for "ID3 data is added/available"
'onload': null, // callback function for "load finished"
'whileloading': null, // callback function for "download progress update" (X of Y bytes received)
'onplay': null, // callback for "play" start
'onpause': null, // callback for "pause"
'onresume': null, // callback for "resume" (pause toggle)
'whileplaying': null, // callback during play (position update)
'onposition': null, // object containing times and function callbacks for positions of interest
'onstop': null, // callback for "user stop"
'onfailure': null, // callback function for when playing fails
'onfinish': null, // callback function for "sound finished playing"
'multiShot': true, // let sounds "restart" or layer on top of each other when played multiple times, rather than one-shot/one at a time
'multiShotEvents': false, // fire multiple sound events (currently onfinish() only) when multiShot is enabled
'position': null, // offset (milliseconds) to seek to within loaded sound data.
'pan': 0, // "pan" settings, left-to-right, -100 to 100
'stream': true, // allows playing before entire file has loaded (recommended)
'to': null, // position to end playback within a sound (msec), default = end
'type': null, // MIME-like hint for file pattern / canPlay() tests, eg. audio/mp3
'usePolicyFile': false, // enable crossdomain.xml request for audio on remote domains (for ID3/waveform access)
'volume': 100 // self-explanatory. 0-100, the latter being the max.
};
this.flash9Options = {
/**
* flash 9-only options,
* merged into defaultOptions if flash 9 is being used
*/
'isMovieStar': null, // "MovieStar" MPEG4 audio mode. Null (default) = auto detect MP4, AAC etc. based on URL. true = force on, ignore URL
'usePeakData': false, // enable left/right channel peak (level) data
'useWaveformData': false, // enable sound spectrum (raw waveform data) - NOTE: May increase CPU load.
'useEQData': false, // enable sound EQ (frequency spectrum data) - NOTE: May increase CPU load.
'onbufferchange': null, // callback for "isBuffering" property change
'ondataerror': null // callback for waveform/eq data access error (flash playing audio in other tabs/domains)
};
this.movieStarOptions = {
/**
* flash 9.0r115+ MPEG4 audio options,
* merged into defaultOptions if flash 9+movieStar mode is enabled
*/
'bufferTime': 3, // seconds of data to buffer before playback begins (null = flash default of 0.1 seconds - if AAC playback is gappy, try increasing.)
'serverURL': null, // rtmp: FMS or FMIS server to connect to, required when requesting media via RTMP or one of its variants
'onconnect': null, // rtmp: callback for connection to flash media server
'duration': null // rtmp: song duration (msec)
};
this.audioFormats = {
/**
* determines HTML5 support + flash requirements.
* if no support (via flash and/or HTML5) for a "required" format, SM2 will fail to start.
* flash fallback is used for MP3 or MP4 if HTML5 can't play it (or if preferFlash = true)
*/
'mp3': {
'type': ['audio/mpeg; codecs="mp3"', 'audio/mpeg', 'audio/mp3', 'audio/MPA', 'audio/mpa-robust'],
'required': true
},
'mp4': {
'related': ['aac','m4a','m4b'], // additional formats under the MP4 container
'type': ['audio/mp4; codecs="mp4a.40.2"', 'audio/aac', 'audio/x-m4a', 'audio/MP4A-LATM', 'audio/mpeg4-generic'],
'required': false
},
'ogg': {
'type': ['audio/ogg; codecs=vorbis'],
'required': false
},
'opus': {
'type': ['audio/ogg; codecs=opus', 'audio/opus'],
'required': false
},
'wav': {
'type': ['audio/wav; codecs="1"', 'audio/wav', 'audio/wave', 'audio/x-wav'],
'required': false
}
};
// HTML attributes (id + class names) for the SWF container
this.movieID = 'sm2-container';
this.id = (smID || 'sm2movie');
this.debugID = 'soundmanager-debug';
this.debugURLParam = /([#?&])debug=1/i;
// dynamic attributes
this.versionNumber = 'V2.97a.20130324';
this.version = null;
this.movieURL = null;
this.altURL = null;
this.swfLoaded = false;
this.enabled = false;
this.oMC = null;
this.sounds = {};
this.soundIDs = [];
this.muted = false;
this.didFlashBlock = false;
this.filePattern = null;
this.filePatterns = {
'flash8': /\.mp3(\?.*)?$/i,
'flash9': /\.mp3(\?.*)?$/i
};
// support indicators, set at init
this.features = {
'buffering': false,
'peakData': false,
'waveformData': false,
'eqData': false,
'movieStar': false
};
// flash sandbox info, used primarily in troubleshooting
this.sandbox = {
// <d>
'type': null,
'types': {
'remote': 'remote (domain-based) rules',
'localWithFile': 'local with file access (no internet access)',
'localWithNetwork': 'local with network (internet access only, no local access)',
'localTrusted': 'local, trusted (local+internet access)'
},
'description': null,
'noRemote': null,
'noLocal': null
// </d>
};
/**
* format support (html5/flash)
* stores canPlayType() results based on audioFormats.
* eg. { mp3: boolean, mp4: boolean }
* treat as read-only.
*/
this.html5 = {
'usingFlash': null // set if/when flash fallback is needed
};
// file type support hash
this.flash = {};
// determined at init time
this.html5Only = false;
// used for special cases (eg. iPad/iPhone/palm OS?)
this.ignoreFlash = false;
/**
* a few private internals (OK, a lot. :D)
*/
var SMSound,
sm2 = this, globalHTML5Audio = null, flash = null, sm = 'soundManager', smc = sm + ': ', h5 = 'HTML5::', id, ua = navigator.userAgent, wl = window.location.href.toString(), doc = document, doNothing, setProperties, init, fV, on_queue = [], debugOpen = true, debugTS, didAppend = false, appendSuccess = false, didInit = false, disabled = false, windowLoaded = false, _wDS, wdCount = 0, initComplete, mixin, assign, extraOptions, addOnEvent, processOnEvents, initUserOnload, delayWaitForEI, waitForEI, setVersionInfo, handleFocus, strings, initMovie, preInit, domContentLoaded, winOnLoad, didDCLoaded, getDocument, createMovie, catchError, setPolling, initDebug, debugLevels = ['log', 'info', 'warn', 'error'], defaultFlashVersion = 8, disableObject, failSafely, normalizeMovieURL, oRemoved = null, oRemovedHTML = null, str, flashBlockHandler, getSWFCSS, swfCSS, toggleDebug, loopFix, policyFix, complain, idCheck, waitingForEI = false, initPending = false, startTimer, stopTimer, timerExecute, h5TimerCount = 0, h5IntervalTimer = null, parseURL, messages = [],
needsFlash = null, featureCheck, html5OK, html5CanPlay, html5Ext, html5Unload, domContentLoadedIE, testHTML5, event, slice = Array.prototype.slice, useGlobalHTML5Audio = false, lastGlobalHTML5URL, hasFlash, detectFlash, badSafariFix, html5_events, showSupport, flushMessages, wrapCallback,
is_iDevice = ua.match(/(ipad|iphone|ipod)/i), isAndroid = ua.match(/android/i), isIE = ua.match(/msie/i), isWebkit = ua.match(/webkit/i), isSafari = (ua.match(/safari/i) && !ua.match(/chrome/i)), isOpera = (ua.match(/opera/i)),
mobileHTML5 = (ua.match(/(mobile|pre\/|xoom)/i) || is_iDevice || isAndroid),
isBadSafari = (!wl.match(/usehtml5audio/i) && !wl.match(/sm2\-ignorebadua/i) && isSafari && !ua.match(/silk/i) && ua.match(/OS X 10_6_([3-7])/i)), // Safari 4 and 5 (excluding Kindle Fire, "Silk") occasionally fail to load/play HTML5 audio on Snow Leopard 10.6.3 through 10.6.7 due to bug(s) in QuickTime X and/or other underlying frameworks. :/ Confirmed bug. https://bugs.webkit.org/show_bug.cgi?id=32159
hasConsole = (window.console !== _undefined && console.log !== _undefined), isFocused = (doc.hasFocus !== _undefined?doc.hasFocus():null), tryInitOnFocus = (isSafari && (doc.hasFocus === _undefined || !doc.hasFocus())), okToDisable = !tryInitOnFocus, flashMIME = /(mp3|mp4|mpa|m4a|m4b)/i,
emptyURL = 'about:blank', // safe URL to unload, or load nothing from (flash 8 + most HTML5 UAs)
overHTTP = (doc.location?doc.location.protocol.match(/http/i):null),
http = (!overHTTP ? 'http:/'+'/' : ''),
// mp3, mp4, aac etc.
netStreamMimeTypes = /^\s*audio\/(?:x-)?(?:mpeg4|aac|flv|mov|mp4||m4v|m4a|m4b|mp4v|3gp|3g2)\s*(?:$|;)/i,
// Flash v9.0r115+ "moviestar" formats
netStreamTypes = ['mpeg4', 'aac', 'flv', 'mov', 'mp4', 'm4v', 'f4v', 'm4a', 'm4b', 'mp4v', '3gp', '3g2'],
netStreamPattern = new RegExp('\\.(' + netStreamTypes.join('|') + ')(\\?.*)?$', 'i');
this.mimePattern = /^\s*audio\/(?:x-)?(?:mp(?:eg|3))\s*(?:$|;)/i; // default mp3 set
// use altURL if not "online"
this.useAltURL = !overHTTP;
swfCSS = {
'swfBox': 'sm2-object-box',
'swfDefault': 'movieContainer',
'swfError': 'swf_error', // SWF loaded, but SM2 couldn't start (other error)
'swfTimedout': 'swf_timedout',
'swfLoaded': 'swf_loaded',
'swfUnblocked': 'swf_unblocked', // or loaded OK
'sm2Debug': 'sm2_debug',
'highPerf': 'high_performance',
'flashDebug': 'flash_debug'
};
/**
* basic HTML5 Audio() support test
* try...catch because of IE 9 "not implemented" nonsense
* https://github.com/Modernizr/Modernizr/issues/224
*/
this.hasHTML5 = (function() {
try {
// new Audio(null) for stupid Opera 9.64 case, which throws not_enough_arguments exception otherwise.
return (Audio !== _undefined && (isOpera && opera !== _undefined && opera.version() < 10 ? new Audio(null) : new Audio()).canPlayType !== _undefined);
} catch(e) {
return false;
}
}());
/**
* Public SoundManager API
* -----------------------
*/
/**
* Configures top-level soundManager properties.
*
* @param {object} options Option parameters, eg. { flashVersion: 9, url: '/path/to/swfs/' }
* onready and ontimeout are also accepted parameters. call soundManager.setup() to see the full list.
*/
this.setup = function(options) {
var noURL = (!sm2.url);
// warn if flash options have already been applied
if (options !== _undefined && didInit && needsFlash && sm2.ok() && (options.flashVersion !== _undefined || options.url !== _undefined || options.html5Test !== _undefined)) {
complain(str('setupLate'));
}
// TODO: defer: true?
assign(options);
// special case 1: "Late setup". SM2 loaded normally, but user didn't assign flash URL eg., setup({url:...}) before SM2 init. Treat as delayed init.
if (noURL && didDCLoaded && options.url !== _undefined) {
sm2.beginDelayedInit();
}
// special case 2: If lazy-loading SM2 (DOMContentLoaded has already happened) and user calls setup() with url: parameter, try to init ASAP.
if (!didDCLoaded && options.url !== _undefined && doc.readyState === 'complete') {
setTimeout(domContentLoaded, 1);
}
return sm2;
};
this.ok = function() {
return (needsFlash?(didInit && !disabled):(sm2.useHTML5Audio && sm2.hasHTML5));
};
this.supported = this.ok; // legacy
this.getMovie = function(smID) {
// safety net: some old browsers differ on SWF references, possibly related to ExternalInterface / flash version
return id(smID) || doc[smID] || window[smID];
};
/**
* Creates a SMSound sound object instance.
*
* @param {object} oOptions Sound options (at minimum, id and url parameters are required.)
* @return {object} SMSound The new SMSound object.
*/
this.createSound = function(oOptions, _url) {
var cs, cs_string, options, oSound = null;
// <d>
cs = sm + '.createSound(): ';
cs_string = cs + str(!didInit?'notReady':'notOK');
// </d>
if (!didInit || !sm2.ok()) {
complain(cs_string);
return false;
}
if (_url !== _undefined) {
// function overloading in JS! :) ..assume simple createSound(id,url) use case
oOptions = {
'id': oOptions,
'url': _url
};
}
// inherit from defaultOptions
options = mixin(oOptions);
options.url = parseURL(options.url);
// <d>
if (options.id.toString().charAt(0).match(/^[0-9]$/)) {
sm2._wD(cs + str('badID', options.id), 2);
}
sm2._wD(cs + options.id + ' (' + options.url + ')', 1);
// </d>
if (idCheck(options.id, true)) {
sm2._wD(cs + options.id + ' exists', 1);
return sm2.sounds[options.id];
}
function make() {
options = loopFix(options);
sm2.sounds[options.id] = new SMSound(options);
sm2.soundIDs.push(options.id);
return sm2.sounds[options.id];
}
if (html5OK(options)) {
oSound = make();
sm2._wD(options.id + ': Using HTML5');
oSound._setup_html5(options);
} else {
if (fV > 8) {
if (options.isMovieStar === null) {
// attempt to detect MPEG-4 formats
options.isMovieStar = !!(options.serverURL || (options.type ? options.type.match(netStreamMimeTypes) : false) || options.url.match(netStreamPattern));
}
// <d>
if (options.isMovieStar) {
sm2._wD(cs + 'using MovieStar handling');
if (options.loops > 1) {
_wDS('noNSLoop');
}
}
// </d>
}
options = policyFix(options, cs);
oSound = make();
if (fV === 8) {
flash._createSound(options.id, options.loops||1, options.usePolicyFile);
} else {
flash._createSound(options.id, options.url, options.usePeakData, options.useWaveformData, options.useEQData, options.isMovieStar, (options.isMovieStar?options.bufferTime:false), options.loops||1, options.serverURL, options.duration||null, options.autoPlay, true, options.autoLoad, options.usePolicyFile);
if (!options.serverURL) {
// We are connected immediately
oSound.connected = true;
if (options.onconnect) {
options.onconnect.apply(oSound);
}
}
}
if (!options.serverURL && (options.autoLoad || options.autoPlay)) {
// call load for non-rtmp streams
oSound.load(options);
}
}
// rtmp will play in onconnect
if (!options.serverURL && options.autoPlay) {
oSound.play();
}
return oSound;
};
/**
* Destroys a SMSound sound object instance.
*
* @param {string} sID The ID of the sound to destroy
*/
this.destroySound = function(sID, _bFromSound) {
// explicitly destroy a sound before normal page unload, etc.
if (!idCheck(sID)) {
return false;
}
var oS = sm2.sounds[sID], i;
// Disable all callbacks while the sound is being destroyed
oS._iO = {};
oS.stop();
oS.unload();
for (i = 0; i < sm2.soundIDs.length; i++) {
if (sm2.soundIDs[i] === sID) {
sm2.soundIDs.splice(i, 1);
break;
}
}
if (!_bFromSound) {
// ignore if being called from SMSound instance
oS.destruct(true);
}
oS = null;
delete sm2.sounds[sID];
return true;
};
/**
* Calls the load() method of a SMSound object by ID.
*
* @param {string} sID The ID of the sound
* @param {object} oOptions Optional: Sound options
*/
this.load = function(sID, oOptions) {
if (!idCheck(sID)) {
return false;
}
return sm2.sounds[sID].load(oOptions);
};
/**
* Calls the unload() method of a SMSound object by ID.
*
* @param {string} sID The ID of the sound
*/
this.unload = function(sID) {
if (!idCheck(sID)) {
return false;
}
return sm2.sounds[sID].unload();
};
/**
* Calls the onPosition() method of a SMSound object by ID.
*
* @param {string} sID The ID of the sound
* @param {number} nPosition The position to watch for
* @param {function} oMethod The relevant callback to fire
* @param {object} oScope Optional: The scope to apply the callback to
* @return {SMSound} The SMSound object
*/
this.onPosition = function(sID, nPosition, oMethod, oScope) {
if (!idCheck(sID)) {
return false;
}
return sm2.sounds[sID].onposition(nPosition, oMethod, oScope);
};
// legacy/backwards-compability: lower-case method name
this.onposition = this.onPosition;
/**
* Calls the clearOnPosition() method of a SMSound object by ID.
*
* @param {string} sID The ID of the sound
* @param {number} nPosition The position to watch for
* @param {function} oMethod Optional: The relevant callback to fire
* @return {SMSound} The SMSound object
*/
this.clearOnPosition = function(sID, nPosition, oMethod) {
if (!idCheck(sID)) {
return false;
}
return sm2.sounds[sID].clearOnPosition(nPosition, oMethod);
};
/**
* Calls the play() method of a SMSound object by ID.
*
* @param {string} sID The ID of the sound
* @param {object} oOptions Optional: Sound options
* @return {SMSound} The SMSound object
*/
this.play = function(sID, oOptions) {
var result = false;
if (!didInit || !sm2.ok()) {
complain(sm + '.play(): ' + str(!didInit?'notReady':'notOK'));
return result;
}
if (!idCheck(sID)) {
if (!(oOptions instanceof Object)) {
// overloading use case: play('mySound','/path/to/some.mp3');
oOptions = {
url: oOptions
};
}
if (oOptions && oOptions.url) {
// overloading use case, create+play: .play('someID',{url:'/path/to.mp3'});
sm2._wD(sm + '.play(): attempting to create "' + sID + '"', 1);
oOptions.id = sID;
result = sm2.createSound(oOptions).play();
}
return result;
}
return sm2.sounds[sID].play(oOptions);
};
this.start = this.play; // just for convenience
/**
* Calls the setPosition() method of a SMSound object by ID.
*
* @param {string} sID The ID of the sound
* @param {number} nMsecOffset Position (milliseconds)
* @return {SMSound} The SMSound object
*/
this.setPosition = function(sID, nMsecOffset) {
if (!idCheck(sID)) {
return false;
}
return sm2.sounds[sID].setPosition(nMsecOffset);
};
/**
* Calls the stop() method of a SMSound object by ID.
*
* @param {string} sID The ID of the sound
* @return {SMSound} The SMSound object
*/
this.stop = function(sID) {
if (!idCheck(sID)) {
return false;
}
sm2._wD(sm + '.stop(' + sID + ')', 1);
return sm2.sounds[sID].stop();
};
/**
* Stops all currently-playing sounds.
*/
this.stopAll = function() {
var oSound;
sm2._wD(sm + '.stopAll()', 1);
for (oSound in sm2.sounds) {
if (sm2.sounds.hasOwnProperty(oSound)) {
// apply only to sound objects
sm2.sounds[oSound].stop();
}
}
};
/**
* Calls the pause() method of a SMSound object by ID.
*
* @param {string} sID The ID of the sound
* @return {SMSound} The SMSound object
*/
this.pause = function(sID) {
if (!idCheck(sID)) {
return false;
}
return sm2.sounds[sID].pause();
};
/**
* Pauses all currently-playing sounds.
*/
this.pauseAll = function() {
var i;
for (i = sm2.soundIDs.length-1; i >= 0; i--) {
sm2.sounds[sm2.soundIDs[i]].pause();
}
};
/**
* Calls the resume() method of a SMSound object by ID.
*
* @param {string} sID The ID of the sound
* @return {SMSound} The SMSound object
*/
this.resume = function(sID) {
if (!idCheck(sID)) {
return false;
}
return sm2.sounds[sID].resume();
};
/**
* Resumes all currently-paused sounds.
*/
this.resumeAll = function() {
var i;
for (i = sm2.soundIDs.length-1; i >= 0; i--) {
sm2.sounds[sm2.soundIDs[i]].resume();
}
};
/**
* Calls the togglePause() method of a SMSound object by ID.
*
* @param {string} sID The ID of the sound
* @return {SMSound} The SMSound object
*/
this.togglePause = function(sID) {
if (!idCheck(sID)) {
return false;
}
return sm2.sounds[sID].togglePause();
};
/**
* Calls the setPan() method of a SMSound object by ID.
*
* @param {string} sID The ID of the sound
* @param {number} nPan The pan value (-100 to 100)
* @return {SMSound} The SMSound object
*/
this.setPan = function(sID, nPan) {
if (!idCheck(sID)) {
return false;
}
return sm2.sounds[sID].setPan(nPan);
};
/**
* Calls the setVolume() method of a SMSound object by ID.
*
* @param {string} sID The ID of the sound
* @param {number} nVol The volume value (0 to 100)
* @return {SMSound} The SMSound object
*/
this.setVolume = function(sID, nVol) {
if (!idCheck(sID)) {
return false;
}
return sm2.sounds[sID].setVolume(nVol);
};
/**
* Calls the mute() method of either a single SMSound object by ID, or all sound objects.
*
* @param {string} sID Optional: The ID of the sound (if omitted, all sounds will be used.)
*/
this.mute = function(sID) {
var i = 0;
if (sID instanceof String) {
sID = null;
}
if (!sID) {
sm2._wD(sm + '.mute(): Muting all sounds');
for (i = sm2.soundIDs.length-1; i >= 0; i--) {
sm2.sounds[sm2.soundIDs[i]].mute();
}
sm2.muted = true;
} else {
if (!idCheck(sID)) {
return false;
}
sm2._wD(sm + '.mute(): Muting "' + sID + '"');
return sm2.sounds[sID].mute();
}
return true;
};
/**
* Mutes all sounds.
*/
this.muteAll = function() {
sm2.mute();
};
/**
* Calls the unmute() method of either a single SMSound object by ID, or all sound objects.
*
* @param {string} sID Optional: The ID of the sound (if omitted, all sounds will be used.)
*/
this.unmute = function(sID) {
var i;
if (sID instanceof String) {
sID = null;
}
if (!sID) {
sm2._wD(sm + '.unmute(): Unmuting all sounds');
for (i = sm2.soundIDs.length-1; i >= 0; i--) {
sm2.sounds[sm2.soundIDs[i]].unmute();
}
sm2.muted = false;
} else {
if (!idCheck(sID)) {
return false;
}
sm2._wD(sm + '.unmute(): Unmuting "' + sID + '"');
return sm2.sounds[sID].unmute();
}
return true;
};
/**
* Unmutes all sounds.
*/
this.unmuteAll = function() {
sm2.unmute();
};
/**
* Calls the toggleMute() method of a SMSound object by ID.
*
* @param {string} sID The ID of the sound
* @return {SMSound} The SMSound object
*/
this.toggleMute = function(sID) {
if (!idCheck(sID)) {
return false;
}
return sm2.sounds[sID].toggleMute();
};
/**
* Retrieves the memory used by the flash plugin.
*
* @return {number} The amount of memory in use
*/
this.getMemoryUse = function() {
// flash-only
var ram = 0;
if (flash && fV !== 8) {
ram = parseInt(flash._getMemoryUse(), 10);
}
return ram;
};
/**
* Undocumented: NOPs soundManager and all SMSound objects.
*/
this.disable = function(bNoDisable) {
// destroy all functions
var i;
if (bNoDisable === _undefined) {
bNoDisable = false;
}
if (disabled) {
return false;
}
disabled = true;
_wDS('shutdown', 1);
for (i = sm2.soundIDs.length-1; i >= 0; i--) {
disableObject(sm2.sounds[sm2.soundIDs[i]]);
}
// fire "complete", despite fail
initComplete(bNoDisable);
event.remove(window, 'load', initUserOnload);
return true;
};
/**
* Determines playability of a MIME type, eg. 'audio/mp3'.
*/
this.canPlayMIME = function(sMIME) {
var result;
if (sm2.hasHTML5) {
result = html5CanPlay({type:sMIME});
}
if (!result && needsFlash) {
// if flash 9, test netStream (movieStar) types as well.
result = (sMIME && sm2.ok() ? !!((fV > 8 ? sMIME.match(netStreamMimeTypes) : null) || sMIME.match(sm2.mimePattern)) : null);
}
return result;
};
/**
* Determines playability of a URL based on audio support.
*
* @param {string} sURL The URL to test
* @return {boolean} URL playability
*/
this.canPlayURL = function(sURL) {
var result;
if (sm2.hasHTML5) {
result = html5CanPlay({url: sURL});
}
if (!result && needsFlash) {
result = (sURL && sm2.ok() ? !!(sURL.match(sm2.filePattern)) : null);
}
return result;
};
/**
* Determines playability of an HTML DOM <a> object (or similar object literal) based on audio support.