soundmanager-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5026 @@
1
+ /** @license
2
+ *
3
+ * SoundManager 2: JavaScript Sound for the Web
4
+ * ----------------------------------------------
5
+ * http://schillmania.com/projects/soundmanager2/
6
+ *
7
+ * Copyright (c) 2007, Scott Schiller. All rights reserved.
8
+ * Code provided under the BSD License:
9
+ * http://schillmania.com/projects/soundmanager2/license.txt
10
+ *
11
+ * V2.97a.20120318
12
+ */
13
+
14
+ /*global window, SM2_DEFER, sm2Debugger, console, document, navigator, setTimeout, setInterval, clearInterval, Audio */
15
+ /*jslint regexp: true, sloppy: true, white: true, nomen: true, plusplus: true */
16
+
17
+ /**
18
+ * About this file
19
+ * ---------------
20
+ * This is the fully-commented source version of the SoundManager 2 API,
21
+ * recommended for use during development and testing.
22
+ *
23
+ * See soundmanager2-nodebug-jsmin.js for an optimized build (~10KB with gzip.)
24
+ * http://schillmania.com/projects/soundmanager2/doc/getstarted/#basic-inclusion
25
+ * Alternately, serve this file with gzip for 75% compression savings (~30KB over HTTP.)
26
+ *
27
+ * You may notice <d> and </d> comments in this source; these are delimiters for
28
+ * debug blocks which are removed in the -nodebug builds, further optimizing code size.
29
+ *
30
+ * Also, as you may note: Whoa, reliable cross-platform/device audio support is hard! ;)
31
+ */
32
+
33
+ (function(window) {
34
+
35
+ var soundManager = null;
36
+
37
+ /**
38
+ * The SoundManager constructor.
39
+ *
40
+ * @constructor
41
+ * @param {string} smURL Optional: Path to SWF files
42
+ * @param {string} smID Optional: The ID to use for the SWF container element
43
+ * @this {SoundManager}
44
+ * @return {SoundManager} The new SoundManager instance
45
+ */
46
+
47
+ function SoundManager(smURL, smID) {
48
+
49
+ // Top-level configuration options
50
+
51
+ this.flashVersion = 8; // flash build to use (8 or 9.) Some API features require 9.
52
+ this.debugMode = true; // enable debugging output (console.log() with HTML fallback)
53
+ this.debugFlash = false; // enable debugging output inside SWF, troubleshoot Flash/browser issues
54
+ this.useConsole = true; // use console.log() if available (otherwise, writes to #soundmanager-debug element)
55
+ this.consoleOnly = true; // if console is being used, do not create/write to #soundmanager-debug
56
+ this.waitForWindowLoad = false; // force SM2 to wait for window.onload() before trying to call soundManager.onload()
57
+ this.bgColor = '#ffffff'; // SWF background color. N/A when wmode = 'transparent'
58
+ this.useHighPerformance = false; // position:fixed flash movie can help increase js/flash speed, minimize lag
59
+ this.flashPollingInterval = null; // msec affecting whileplaying/loading callback frequency. If null, default of 50 msec is used.
60
+ this.html5PollingInterval = null; // msec affecting whileplaying() for HTML5 audio, excluding mobile devices. If null, native HTML5 update events are used.
61
+ this.flashLoadTimeout = 1000; // msec to wait for flash movie to load before failing (0 = infinity)
62
+ this.wmode = null; // flash rendering mode - null, 'transparent', or 'opaque' (last two allow z-index to work)
63
+ this.allowScriptAccess = 'always'; // for scripting the SWF (object/embed property), 'always' or 'sameDomain'
64
+ this.useFlashBlock = false; // *requires flashblock.css, see demos* - allow recovery from flash blockers. Wait indefinitely and apply timeout CSS to SWF, if applicable.
65
+ this.useHTML5Audio = true; // use HTML5 Audio() where API is supported (most Safari, Chrome versions), Firefox (no MP3/MP4.) Ideally, transparent vs. Flash API where possible.
66
+ this.html5Test = /^(probably|maybe)$/i; // HTML5 Audio() format support test. Use /^probably$/i; if you want to be more conservative.
67
+ this.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.
68
+ this.noSWFCache = false; // if true, appends ?ts={date} to break aggressive SWF caching.
69
+
70
+ this.audioFormats = {
71
+
72
+ /**
73
+ * determines HTML5 support + flash requirements.
74
+ * if no support (via flash and/or HTML5) for a "required" format, SM2 will fail to start.
75
+ * flash fallback is used for MP3 or MP4 if HTML5 can't play it (or if preferFlash = true)
76
+ * multiple MIME types may be tested while trying to get a positive canPlayType() response.
77
+ */
78
+
79
+ 'mp3': {
80
+ 'type': ['audio/mpeg; codecs="mp3"', 'audio/mpeg', 'audio/mp3', 'audio/MPA', 'audio/mpa-robust'],
81
+ 'required': true
82
+ },
83
+
84
+ 'mp4': {
85
+ 'related': ['aac','m4a'], // additional formats under the MP4 container
86
+ 'type': ['audio/mp4; codecs="mp4a.40.2"', 'audio/aac', 'audio/x-m4a', 'audio/MP4A-LATM', 'audio/mpeg4-generic'],
87
+ 'required': false
88
+ },
89
+
90
+ 'ogg': {
91
+ 'type': ['audio/ogg; codecs=vorbis'],
92
+ 'required': false
93
+ },
94
+
95
+ 'wav': {
96
+ 'type': ['audio/wav; codecs="1"', 'audio/wav', 'audio/wave', 'audio/x-wav'],
97
+ 'required': false
98
+ }
99
+
100
+ };
101
+
102
+ this.defaultOptions = {
103
+
104
+ /**
105
+ * the default configuration for sound objects made with createSound() and related methods
106
+ * eg., volume, auto-load behaviour and so forth
107
+ */
108
+
109
+ '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)
110
+ 'autoPlay': false, // enable playing of file as soon as possible (much faster if "stream" is true)
111
+ 'from': null, // position to start playback within a sound (msec), default = beginning
112
+ 'loops': 1, // how many times to repeat the sound (position will wrap around to 0, setPosition() will break out of loop when >0)
113
+ 'onid3': null, // callback function for "ID3 data is added/available"
114
+ 'onload': null, // callback function for "load finished"
115
+ 'whileloading': null, // callback function for "download progress update" (X of Y bytes received)
116
+ 'onplay': null, // callback for "play" start
117
+ 'onpause': null, // callback for "pause"
118
+ 'onresume': null, // callback for "resume" (pause toggle)
119
+ 'whileplaying': null, // callback during play (position update)
120
+ 'onposition': null, // object containing times and function callbacks for positions of interest
121
+ 'onstop': null, // callback for "user stop"
122
+ 'onfailure': null, // callback function for when playing fails
123
+ 'onfinish': null, // callback function for "sound finished playing"
124
+ 'multiShot': true, // let sounds "restart" or layer on top of each other when played multiple times, rather than one-shot/one at a time
125
+ 'multiShotEvents': false, // fire multiple sound events (currently onfinish() only) when multiShot is enabled
126
+ 'position': null, // offset (milliseconds) to seek to within loaded sound data.
127
+ 'pan': 0, // "pan" settings, left-to-right, -100 to 100
128
+ 'stream': true, // allows playing before entire file has loaded (recommended)
129
+ 'to': null, // position to end playback within a sound (msec), default = end
130
+ 'type': null, // MIME-like hint for file pattern / canPlay() tests, eg. audio/mp3
131
+ 'usePolicyFile': false, // enable crossdomain.xml request for audio on remote domains (for ID3/waveform access)
132
+ 'volume': 100 // self-explanatory. 0-100, the latter being the max.
133
+
134
+ };
135
+
136
+ this.flash9Options = {
137
+
138
+ /**
139
+ * flash 9-only options,
140
+ * merged into defaultOptions if flash 9 is being used
141
+ */
142
+
143
+ 'isMovieStar': null, // "MovieStar" MPEG4 audio mode. Null (default) = auto detect MP4, AAC etc. based on URL. true = force on, ignore URL
144
+ 'usePeakData': false, // enable left/right channel peak (level) data
145
+ 'useWaveformData': false, // enable sound spectrum (raw waveform data) - NOTE: May increase CPU load.
146
+ 'useEQData': false, // enable sound EQ (frequency spectrum data) - NOTE: May increase CPU load.
147
+ 'onbufferchange': null, // callback for "isBuffering" property change
148
+ 'ondataerror': null // callback for waveform/eq data access error (flash playing audio in other tabs/domains)
149
+
150
+ };
151
+
152
+ this.movieStarOptions = {
153
+
154
+ /**
155
+ * flash 9.0r115+ MPEG4 audio options,
156
+ * merged into defaultOptions if flash 9+movieStar mode is enabled
157
+ */
158
+
159
+ 'bufferTime': 3, // seconds of data to buffer before playback begins (null = flash default of 0.1 seconds - if AAC playback is gappy, try increasing.)
160
+ 'serverURL': null, // rtmp: FMS or FMIS server to connect to, required when requesting media via RTMP or one of its variants
161
+ 'onconnect': null, // rtmp: callback for connection to flash media server
162
+ 'duration': null // rtmp: song duration (msec)
163
+
164
+ };
165
+
166
+ // HTML attributes (id + class names) for the SWF container
167
+
168
+ this.movieID = 'sm2-container';
169
+ this.id = (smID || 'sm2movie');
170
+
171
+ this.debugID = 'soundmanager-debug';
172
+ this.debugURLParam = /([#?&])debug=1/i;
173
+
174
+ // dynamic attributes
175
+
176
+ this.versionNumber = 'V2.97a.20120318';
177
+ this.version = null;
178
+ this.movieURL = null;
179
+ this.url = (smURL || null);
180
+ this.altURL = null;
181
+ this.swfLoaded = false;
182
+ this.enabled = false;
183
+ this.oMC = null;
184
+ this.sounds = {};
185
+ this.soundIDs = [];
186
+ this.muted = false;
187
+ this.didFlashBlock = false;
188
+ this.filePattern = null;
189
+
190
+ this.filePatterns = {
191
+
192
+ 'flash8': /\.mp3(\?.*)?$/i,
193
+ 'flash9': /\.mp3(\?.*)?$/i
194
+
195
+ };
196
+
197
+ // support indicators, set at init
198
+
199
+ this.features = {
200
+
201
+ 'buffering': false,
202
+ 'peakData': false,
203
+ 'waveformData': false,
204
+ 'eqData': false,
205
+ 'movieStar': false
206
+
207
+ };
208
+
209
+ // flash sandbox info, used primarily in troubleshooting
210
+
211
+ this.sandbox = {
212
+
213
+ // <d>
214
+ 'type': null,
215
+ 'types': {
216
+ 'remote': 'remote (domain-based) rules',
217
+ 'localWithFile': 'local with file access (no internet access)',
218
+ 'localWithNetwork': 'local with network (internet access only, no local access)',
219
+ 'localTrusted': 'local, trusted (local+internet access)'
220
+ },
221
+ 'description': null,
222
+ 'noRemote': null,
223
+ 'noLocal': null
224
+ // </d>
225
+
226
+ };
227
+
228
+ /**
229
+ * basic HTML5 Audio() support test
230
+ * try...catch because of IE 9 "not implemented" nonsense
231
+ * https://github.com/Modernizr/Modernizr/issues/224
232
+ */
233
+
234
+ this.hasHTML5 = (function() {
235
+ try {
236
+ return (typeof Audio !== 'undefined' && typeof new Audio().canPlayType !== 'undefined');
237
+ } catch(e) {
238
+ return false;
239
+ }
240
+ }());
241
+
242
+ /**
243
+ * format support (html5/flash)
244
+ * stores canPlayType() results based on audioFormats.
245
+ * eg. { mp3: boolean, mp4: boolean }
246
+ * treat as read-only.
247
+ */
248
+
249
+ this.html5 = {
250
+ 'usingFlash': null // set if/when flash fallback is needed
251
+ };
252
+
253
+ this.flash = {}; // file type support hash
254
+
255
+ this.html5Only = false; // determined at init time
256
+ this.ignoreFlash = false; // used for special cases (eg. iPad/iPhone/palm OS?)
257
+
258
+ /**
259
+ * a few private internals (OK, a lot. :D)
260
+ */
261
+
262
+ var SMSound,
263
+ _s = this, _flash = null, _sm = 'soundManager', _smc = _sm+'::', _h5 = 'HTML5::', _id, _ua = navigator.userAgent, _win = window, _wl = _win.location.href.toString(), _doc = document, _doNothing, _init, _fV, _on_queue = [], _debugOpen = true, _debugTS, _didAppend = false, _appendSuccess = false, _didInit = false, _disabled = false, _windowLoaded = false, _wDS, _wdCount = 0, _initComplete, _mixin, _addOnEvent, _processOnEvents, _initUserOnload, _delayWaitForEI, _waitForEI, _setVersionInfo, _handleFocus, _strings, _initMovie, _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,
264
+ _needsFlash = null, _featureCheck, _html5OK, _html5CanPlay, _html5Ext, _html5Unload, _domContentLoadedIE, _testHTML5, _event, _slice = Array.prototype.slice, _useGlobalHTML5Audio = false, _hasFlash, _detectFlash, _badSafariFix, _html5_events, _showSupport,
265
+ _is_iDevice = _ua.match(/(ipad|iphone|ipod)/i), _is_firefox = _ua.match(/firefox/i), _is_android = _ua.match(/droid/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)),
266
+ _likesHTML5 = (_ua.match(/(mobile|pre\/|xoom)/i) || _is_iDevice),
267
+ _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
268
+ _hasConsole = (typeof console !== 'undefined' && typeof console.log !== 'undefined'), _isFocused = (typeof _doc.hasFocus !== 'undefined'?_doc.hasFocus():null), _tryInitOnFocus = (_isSafari && typeof _doc.hasFocus === 'undefined'), _okToDisable = !_tryInitOnFocus, _flashMIME = /(mp3|mp4|mpa)/i,
269
+ _emptyURL = 'about:blank', // safe URL to unload, or load nothing from (flash 8 + most HTML5 UAs)
270
+ _overHTTP = (_doc.location?_doc.location.protocol.match(/http/i):null),
271
+ _http = (!_overHTTP ? 'http:/'+'/' : ''),
272
+ // mp3, mp4, aac etc.
273
+ _netStreamMimeTypes = /^\s*audio\/(?:x-)?(?:mpeg4|aac|flv|mov|mp4||m4v|m4a|mp4v|3gp|3g2)\s*(?:$|;)/i,
274
+ // Flash v9.0r115+ "moviestar" formats
275
+ _netStreamTypes = ['mpeg4', 'aac', 'flv', 'mov', 'mp4', 'm4v', 'f4v', 'm4a', 'mp4v', '3gp', '3g2'],
276
+ _netStreamPattern = new RegExp('\\.(' + _netStreamTypes.join('|') + ')(\\?.*)?$', 'i');
277
+
278
+ this.mimePattern = /^\s*audio\/(?:x-)?(?:mp(?:eg|3))\s*(?:$|;)/i; // default mp3 set
279
+
280
+ // use altURL if not "online"
281
+ this.useAltURL = !_overHTTP;
282
+ this._global_a = null;
283
+
284
+ _swfCSS = {
285
+
286
+ 'swfBox': 'sm2-object-box',
287
+ 'swfDefault': 'movieContainer',
288
+ 'swfError': 'swf_error', // SWF loaded, but SM2 couldn't start (other error)
289
+ 'swfTimedout': 'swf_timedout',
290
+ 'swfLoaded': 'swf_loaded',
291
+ 'swfUnblocked': 'swf_unblocked', // or loaded OK
292
+ 'sm2Debug': 'sm2_debug',
293
+ 'highPerf': 'high_performance',
294
+ 'flashDebug': 'flash_debug'
295
+
296
+ };
297
+
298
+ if (_likesHTML5) {
299
+
300
+ // prefer HTML5 for mobile + tablet-like devices, probably more reliable vs. flash at this point.
301
+ _s.useHTML5Audio = true;
302
+ _s.preferFlash = false;
303
+
304
+ if (_is_iDevice) {
305
+ // by default, use global feature. iOS onfinish() -> next may fail otherwise.
306
+ _s.ignoreFlash = true;
307
+ _useGlobalHTML5Audio = true;
308
+ }
309
+
310
+ }
311
+
312
+ /**
313
+ * Public SoundManager API
314
+ * -----------------------
315
+ */
316
+
317
+ this.ok = function() {
318
+
319
+ return (_needsFlash?(_didInit && !_disabled):(_s.useHTML5Audio && _s.hasHTML5));
320
+
321
+ };
322
+
323
+ this.supported = this.ok; // legacy
324
+
325
+ this.getMovie = function(smID) {
326
+
327
+ // safety net: some old browsers differ on SWF references, possibly related to ExternalInterface / flash version
328
+ return _id(smID) || _doc[smID] || _win[smID];
329
+
330
+ };
331
+
332
+ /**
333
+ * Creates a SMSound sound object instance.
334
+ *
335
+ * @param {object} oOptions Sound options (at minimum, id and url are required.)
336
+ * @return {object} SMSound The new SMSound object.
337
+ */
338
+
339
+ this.createSound = function(oOptions) {
340
+
341
+ var _cs, _cs_string,
342
+ thisOptions = null, oSound = null, _tO = null;
343
+
344
+ // <d>
345
+ _cs = _sm+'.createSound(): ';
346
+ _cs_string = _cs + _str(!_didInit?'notReady':'notOK');
347
+ // </d>
348
+
349
+ if (!_didInit || !_s.ok()) {
350
+ _complain(_cs_string);
351
+ return false;
352
+ }
353
+
354
+ if (arguments.length === 2) {
355
+ // function overloading in JS! :) ..assume simple createSound(id,url) use case
356
+ oOptions = {
357
+ 'id': arguments[0],
358
+ 'url': arguments[1]
359
+ };
360
+ }
361
+
362
+ // inherit from defaultOptions
363
+ thisOptions = _mixin(oOptions);
364
+
365
+ thisOptions.url = _parseURL(thisOptions.url);
366
+
367
+ // local shortcut
368
+ _tO = thisOptions;
369
+
370
+ // <d>
371
+ if (_tO.id.toString().charAt(0).match(/^[0-9]$/)) {
372
+ _s._wD(_cs + _str('badID', _tO.id), 2);
373
+ }
374
+
375
+ _s._wD(_cs + _tO.id + ' (' + _tO.url + ')', 1);
376
+ // </d>
377
+
378
+ if (_idCheck(_tO.id, true)) {
379
+ _s._wD(_cs + _tO.id + ' exists', 1);
380
+ return _s.sounds[_tO.id];
381
+ }
382
+
383
+ function make() {
384
+
385
+ thisOptions = _loopFix(thisOptions);
386
+ _s.sounds[_tO.id] = new SMSound(_tO);
387
+ _s.soundIDs.push(_tO.id);
388
+ return _s.sounds[_tO.id];
389
+
390
+ }
391
+
392
+ if (_html5OK(_tO)) {
393
+
394
+ oSound = make();
395
+ _s._wD('Loading sound '+_tO.id+' via HTML5');
396
+ oSound._setup_html5(_tO);
397
+
398
+ } else {
399
+
400
+ if (_fV > 8) {
401
+ if (_tO.isMovieStar === null) {
402
+ // attempt to detect MPEG-4 formats
403
+ _tO.isMovieStar = (_tO.serverURL || (_tO.type ? _tO.type.match(_netStreamMimeTypes) : false) || _tO.url.match(_netStreamPattern));
404
+ }
405
+ // <d>
406
+ if (_tO.isMovieStar) {
407
+ _s._wD(_cs + 'using MovieStar handling');
408
+ }
409
+ // </d>
410
+ if (_tO.isMovieStar) {
411
+ if (_tO.usePeakData) {
412
+ _wDS('noPeak');
413
+ _tO.usePeakData = false;
414
+ }
415
+ // <d>
416
+ if (_tO.loops > 1) {
417
+ _wDS('noNSLoop');
418
+ }
419
+ // </d>
420
+ }
421
+ }
422
+
423
+ _tO = _policyFix(_tO, _cs);
424
+ oSound = make();
425
+
426
+ if (_fV === 8) {
427
+ _flash._createSound(_tO.id, _tO.loops||1, _tO.usePolicyFile);
428
+ } else {
429
+ _flash._createSound(_tO.id, _tO.url, _tO.usePeakData, _tO.useWaveformData, _tO.useEQData, _tO.isMovieStar, (_tO.isMovieStar?_tO.bufferTime:false), _tO.loops||1, _tO.serverURL, _tO.duration||null, _tO.autoPlay, true, _tO.autoLoad, _tO.usePolicyFile);
430
+ if (!_tO.serverURL) {
431
+ // We are connected immediately
432
+ oSound.connected = true;
433
+ if (_tO.onconnect) {
434
+ _tO.onconnect.apply(oSound);
435
+ }
436
+ }
437
+ }
438
+
439
+ if (!_tO.serverURL && (_tO.autoLoad || _tO.autoPlay)) {
440
+ // call load for non-rtmp streams
441
+ oSound.load(_tO);
442
+ }
443
+
444
+ }
445
+
446
+ // rtmp will play in onconnect
447
+ if (!_tO.serverURL && _tO.autoPlay) {
448
+ oSound.play();
449
+ }
450
+
451
+ return oSound;
452
+
453
+ };
454
+
455
+ /**
456
+ * Destroys a SMSound sound object instance.
457
+ *
458
+ * @param {string} sID The ID of the sound to destroy
459
+ */
460
+
461
+ this.destroySound = function(sID, _bFromSound) {
462
+
463
+ // explicitly destroy a sound before normal page unload, etc.
464
+
465
+ if (!_idCheck(sID)) {
466
+ return false;
467
+ }
468
+
469
+ var oS = _s.sounds[sID], i;
470
+
471
+ // Disable all callbacks while the sound is being destroyed
472
+ oS._iO = {};
473
+
474
+ oS.stop();
475
+ oS.unload();
476
+
477
+ for (i = 0; i < _s.soundIDs.length; i++) {
478
+ if (_s.soundIDs[i] === sID) {
479
+ _s.soundIDs.splice(i, 1);
480
+ break;
481
+ }
482
+ }
483
+
484
+ if (!_bFromSound) {
485
+ // ignore if being called from SMSound instance
486
+ oS.destruct(true);
487
+ }
488
+
489
+ oS = null;
490
+ delete _s.sounds[sID];
491
+
492
+ return true;
493
+
494
+ };
495
+
496
+ /**
497
+ * Calls the load() method of a SMSound object by ID.
498
+ *
499
+ * @param {string} sID The ID of the sound
500
+ * @param {object} oOptions Optional: Sound options
501
+ */
502
+
503
+ this.load = function(sID, oOptions) {
504
+
505
+ if (!_idCheck(sID)) {
506
+ return false;
507
+ }
508
+ return _s.sounds[sID].load(oOptions);
509
+
510
+ };
511
+
512
+ /**
513
+ * Calls the unload() method of a SMSound object by ID.
514
+ *
515
+ * @param {string} sID The ID of the sound
516
+ */
517
+
518
+ this.unload = function(sID) {
519
+
520
+ if (!_idCheck(sID)) {
521
+ return false;
522
+ }
523
+ return _s.sounds[sID].unload();
524
+
525
+ };
526
+
527
+ /**
528
+ * Calls the onPosition() method of a SMSound object by ID.
529
+ *
530
+ * @param {string} sID The ID of the sound
531
+ * @param {number} nPosition The position to watch for
532
+ * @param {function} oMethod The relevant callback to fire
533
+ * @param {object} oScope Optional: The scope to apply the callback to
534
+ * @return {SMSound} The SMSound object
535
+ */
536
+
537
+ this.onPosition = function(sID, nPosition, oMethod, oScope) {
538
+
539
+ if (!_idCheck(sID)) {
540
+ return false;
541
+ }
542
+ return _s.sounds[sID].onposition(nPosition, oMethod, oScope);
543
+
544
+ };
545
+
546
+ // legacy/backwards-compability: lower-case method name
547
+ this.onposition = this.onPosition;
548
+
549
+ /**
550
+ * Calls the clearOnPosition() method of a SMSound object by ID.
551
+ *
552
+ * @param {string} sID The ID of the sound
553
+ * @param {number} nPosition The position to watch for
554
+ * @param {function} oMethod Optional: The relevant callback to fire
555
+ * @return {SMSound} The SMSound object
556
+ */
557
+
558
+ this.clearOnPosition = function(sID, nPosition, oMethod) {
559
+
560
+ if (!_idCheck(sID)) {
561
+ return false;
562
+ }
563
+ return _s.sounds[sID].clearOnPosition(nPosition, oMethod);
564
+
565
+ };
566
+
567
+ /**
568
+ * Calls the play() method of a SMSound object by ID.
569
+ *
570
+ * @param {string} sID The ID of the sound
571
+ * @param {object} oOptions Optional: Sound options
572
+ * @return {SMSound} The SMSound object
573
+ */
574
+
575
+ this.play = function(sID, oOptions) {
576
+
577
+ if (!_didInit || !_s.ok()) {
578
+ _complain(_sm+'.play(): ' + _str(!_didInit?'notReady':'notOK'));
579
+ return false;
580
+ }
581
+
582
+ if (!_idCheck(sID)) {
583
+ if (!(oOptions instanceof Object)) {
584
+ // overloading use case: play('mySound','/path/to/some.mp3');
585
+ oOptions = {
586
+ url: oOptions
587
+ };
588
+ }
589
+ if (oOptions && oOptions.url) {
590
+ // overloading use case, create+play: .play('someID',{url:'/path/to.mp3'});
591
+ _s._wD(_sm+'.play(): attempting to create "' + sID + '"', 1);
592
+ oOptions.id = sID;
593
+ return _s.createSound(oOptions).play();
594
+ } else {
595
+ return false;
596
+ }
597
+ }
598
+
599
+ return _s.sounds[sID].play(oOptions);
600
+
601
+ };
602
+
603
+ this.start = this.play; // just for convenience
604
+
605
+ /**
606
+ * Calls the setPosition() method of a SMSound object by ID.
607
+ *
608
+ * @param {string} sID The ID of the sound
609
+ * @param {number} nMsecOffset Position (milliseconds)
610
+ * @return {SMSound} The SMSound object
611
+ */
612
+
613
+ this.setPosition = function(sID, nMsecOffset) {
614
+
615
+ if (!_idCheck(sID)) {
616
+ return false;
617
+ }
618
+ return _s.sounds[sID].setPosition(nMsecOffset);
619
+
620
+ };
621
+
622
+ /**
623
+ * Calls the stop() method of a SMSound object by ID.
624
+ *
625
+ * @param {string} sID The ID of the sound
626
+ * @return {SMSound} The SMSound object
627
+ */
628
+
629
+ this.stop = function(sID) {
630
+
631
+ if (!_idCheck(sID)) {
632
+ return false;
633
+ }
634
+
635
+ _s._wD(_sm+'.stop(' + sID + ')', 1);
636
+ return _s.sounds[sID].stop();
637
+
638
+ };
639
+
640
+ /**
641
+ * Stops all currently-playing sounds.
642
+ */
643
+
644
+ this.stopAll = function() {
645
+
646
+ var oSound;
647
+ _s._wD(_sm+'.stopAll()', 1);
648
+
649
+ for (oSound in _s.sounds) {
650
+ if (_s.sounds.hasOwnProperty(oSound)) {
651
+ // apply only to sound objects
652
+ _s.sounds[oSound].stop();
653
+ }
654
+ }
655
+
656
+ };
657
+
658
+ /**
659
+ * Calls the pause() method of a SMSound object by ID.
660
+ *
661
+ * @param {string} sID The ID of the sound
662
+ * @return {SMSound} The SMSound object
663
+ */
664
+
665
+ this.pause = function(sID) {
666
+
667
+ if (!_idCheck(sID)) {
668
+ return false;
669
+ }
670
+ return _s.sounds[sID].pause();
671
+
672
+ };
673
+
674
+ /**
675
+ * Pauses all currently-playing sounds.
676
+ */
677
+
678
+ this.pauseAll = function() {
679
+
680
+ var i;
681
+ for (i = _s.soundIDs.length-1; i >= 0; i--) {
682
+ _s.sounds[_s.soundIDs[i]].pause();
683
+ }
684
+
685
+ };
686
+
687
+ /**
688
+ * Calls the resume() method of a SMSound object by ID.
689
+ *
690
+ * @param {string} sID The ID of the sound
691
+ * @return {SMSound} The SMSound object
692
+ */
693
+
694
+ this.resume = function(sID) {
695
+
696
+ if (!_idCheck(sID)) {
697
+ return false;
698
+ }
699
+ return _s.sounds[sID].resume();
700
+
701
+ };
702
+
703
+ /**
704
+ * Resumes all currently-paused sounds.
705
+ */
706
+
707
+ this.resumeAll = function() {
708
+
709
+ var i;
710
+ for (i = _s.soundIDs.length-1; i >= 0; i--) {
711
+ _s.sounds[_s.soundIDs[i]].resume();
712
+ }
713
+
714
+ };
715
+
716
+ /**
717
+ * Calls the togglePause() method of a SMSound object by ID.
718
+ *
719
+ * @param {string} sID The ID of the sound
720
+ * @return {SMSound} The SMSound object
721
+ */
722
+
723
+ this.togglePause = function(sID) {
724
+
725
+ if (!_idCheck(sID)) {
726
+ return false;
727
+ }
728
+ return _s.sounds[sID].togglePause();
729
+
730
+ };
731
+
732
+ /**
733
+ * Calls the setPan() method of a SMSound object by ID.
734
+ *
735
+ * @param {string} sID The ID of the sound
736
+ * @param {number} nPan The pan value (-100 to 100)
737
+ * @return {SMSound} The SMSound object
738
+ */
739
+
740
+ this.setPan = function(sID, nPan) {
741
+
742
+ if (!_idCheck(sID)) {
743
+ return false;
744
+ }
745
+ return _s.sounds[sID].setPan(nPan);
746
+
747
+ };
748
+
749
+ /**
750
+ * Calls the setVolume() method of a SMSound object by ID.
751
+ *
752
+ * @param {string} sID The ID of the sound
753
+ * @param {number} nVol The volume value (0 to 100)
754
+ * @return {SMSound} The SMSound object
755
+ */
756
+
757
+ this.setVolume = function(sID, nVol) {
758
+
759
+ if (!_idCheck(sID)) {
760
+ return false;
761
+ }
762
+ return _s.sounds[sID].setVolume(nVol);
763
+
764
+ };
765
+
766
+ /**
767
+ * Calls the mute() method of either a single SMSound object by ID, or all sound objects.
768
+ *
769
+ * @param {string} sID Optional: The ID of the sound (if omitted, all sounds will be used.)
770
+ */
771
+
772
+ this.mute = function(sID) {
773
+
774
+ var i = 0;
775
+
776
+ if (typeof sID !== 'string') {
777
+ sID = null;
778
+ }
779
+
780
+ if (!sID) {
781
+ _s._wD(_sm+'.mute(): Muting all sounds');
782
+ for (i = _s.soundIDs.length-1; i >= 0; i--) {
783
+ _s.sounds[_s.soundIDs[i]].mute();
784
+ }
785
+ _s.muted = true;
786
+ } else {
787
+ if (!_idCheck(sID)) {
788
+ return false;
789
+ }
790
+ _s._wD(_sm+'.mute(): Muting "' + sID + '"');
791
+ return _s.sounds[sID].mute();
792
+ }
793
+
794
+ return true;
795
+
796
+ };
797
+
798
+ /**
799
+ * Mutes all sounds.
800
+ */
801
+
802
+ this.muteAll = function() {
803
+
804
+ _s.mute();
805
+
806
+ };
807
+
808
+ /**
809
+ * Calls the unmute() method of either a single SMSound object by ID, or all sound objects.
810
+ *
811
+ * @param {string} sID Optional: The ID of the sound (if omitted, all sounds will be used.)
812
+ */
813
+
814
+ this.unmute = function(sID) {
815
+
816
+ var i;
817
+
818
+ if (typeof sID !== 'string') {
819
+ sID = null;
820
+ }
821
+
822
+ if (!sID) {
823
+
824
+ _s._wD(_sm+'.unmute(): Unmuting all sounds');
825
+ for (i = _s.soundIDs.length-1; i >= 0; i--) {
826
+ _s.sounds[_s.soundIDs[i]].unmute();
827
+ }
828
+ _s.muted = false;
829
+
830
+ } else {
831
+
832
+ if (!_idCheck(sID)) {
833
+ return false;
834
+ }
835
+ _s._wD(_sm+'.unmute(): Unmuting "' + sID + '"');
836
+ return _s.sounds[sID].unmute();
837
+
838
+ }
839
+
840
+ return true;
841
+
842
+ };
843
+
844
+ /**
845
+ * Unmutes all sounds.
846
+ */
847
+
848
+ this.unmuteAll = function() {
849
+
850
+ _s.unmute();
851
+
852
+ };
853
+
854
+ /**
855
+ * Calls the toggleMute() method of a SMSound object by ID.
856
+ *
857
+ * @param {string} sID The ID of the sound
858
+ * @return {SMSound} The SMSound object
859
+ */
860
+
861
+ this.toggleMute = function(sID) {
862
+
863
+ if (!_idCheck(sID)) {
864
+ return false;
865
+ }
866
+ return _s.sounds[sID].toggleMute();
867
+
868
+ };
869
+
870
+ /**
871
+ * Retrieves the memory used by the flash plugin.
872
+ *
873
+ * @return {number} The amount of memory in use
874
+ */
875
+
876
+ this.getMemoryUse = function() {
877
+
878
+ // flash-only
879
+ var ram = 0;
880
+
881
+ if (_flash && _fV !== 8) {
882
+ ram = parseInt(_flash._getMemoryUse(), 10);
883
+ }
884
+
885
+ return ram;
886
+
887
+ };
888
+
889
+ /**
890
+ * Undocumented: NOPs soundManager and all SMSound objects.
891
+ */
892
+
893
+ this.disable = function(bNoDisable) {
894
+
895
+ // destroy all functions
896
+ var i;
897
+
898
+ if (typeof bNoDisable === 'undefined') {
899
+ bNoDisable = false;
900
+ }
901
+
902
+ if (_disabled) {
903
+ return false;
904
+ }
905
+
906
+ _disabled = true;
907
+ _wDS('shutdown', 1);
908
+
909
+ for (i = _s.soundIDs.length-1; i >= 0; i--) {
910
+ _disableObject(_s.sounds[_s.soundIDs[i]]);
911
+ }
912
+
913
+ // fire "complete", despite fail
914
+ _initComplete(bNoDisable);
915
+ _event.remove(_win, 'load', _initUserOnload);
916
+
917
+ return true;
918
+
919
+ };
920
+
921
+ /**
922
+ * Determines playability of a MIME type, eg. 'audio/mp3'.
923
+ */
924
+
925
+ this.canPlayMIME = function(sMIME) {
926
+
927
+ var result;
928
+
929
+ if (_s.hasHTML5) {
930
+ result = _html5CanPlay({type:sMIME});
931
+ }
932
+
933
+ if (!_needsFlash || result) {
934
+ // no flash, or OK
935
+ return result;
936
+ } else {
937
+ // if flash 9, test netStream (movieStar) types as well.
938
+ return (sMIME && _s.ok() ? !!((_fV > 8 ? sMIME.match(_netStreamMimeTypes) : null) || sMIME.match(_s.mimePattern)) : null);
939
+ }
940
+
941
+ };
942
+
943
+ /**
944
+ * Determines playability of a URL based on audio support.
945
+ *
946
+ * @param {string} sURL The URL to test
947
+ * @return {boolean} URL playability
948
+ */
949
+
950
+ this.canPlayURL = function(sURL) {
951
+
952
+ var result;
953
+
954
+ if (_s.hasHTML5) {
955
+ result = _html5CanPlay({url: sURL});
956
+ }
957
+
958
+ if (!_needsFlash || result) {
959
+ // no flash, or OK
960
+ return result;
961
+ } else {
962
+ return (sURL && _s.ok() ? !!(sURL.match(_s.filePattern)) : null);
963
+ }
964
+
965
+ };
966
+
967
+ /**
968
+ * Determines playability of an HTML DOM &lt;a&gt; object (or similar object literal) based on audio support.
969
+ *
970
+ * @param {object} oLink an HTML DOM &lt;a&gt; object or object literal including href and/or type attributes
971
+ * @return {boolean} URL playability
972
+ */
973
+
974
+ this.canPlayLink = function(oLink) {
975
+
976
+ if (typeof oLink.type !== 'undefined' && oLink.type) {
977
+ if (_s.canPlayMIME(oLink.type)) {
978
+ return true;
979
+ }
980
+ }
981
+
982
+ return _s.canPlayURL(oLink.href);
983
+
984
+ };
985
+
986
+ /**
987
+ * Retrieves a SMSound object by ID.
988
+ *
989
+ * @param {string} sID The ID of the sound
990
+ * @return {SMSound} The SMSound object
991
+ */
992
+
993
+ this.getSoundById = function(sID, _suppressDebug) {
994
+
995
+ if (!sID) {
996
+ throw new Error(_sm+'.getSoundById(): sID is null/undefined');
997
+ }
998
+
999
+ var result = _s.sounds[sID];
1000
+
1001
+ // <d>
1002
+ if (!result && !_suppressDebug) {
1003
+ _s._wD('"' + sID + '" is an invalid sound ID.', 2);
1004
+ }
1005
+ // </d>
1006
+
1007
+ return result;
1008
+
1009
+ };
1010
+
1011
+ /**
1012
+ * Queues a callback for execution when SoundManager has successfully initialized.
1013
+ *
1014
+ * @param {function} oMethod The callback method to fire
1015
+ * @param {object} oScope Optional: The scope to apply to the callback
1016
+ */
1017
+
1018
+ this.onready = function(oMethod, oScope) {
1019
+
1020
+ var sType = 'onready';
1021
+
1022
+ if (oMethod && oMethod instanceof Function) {
1023
+
1024
+ // <d>
1025
+ if (_didInit) {
1026
+ _s._wD(_str('queue', sType));
1027
+ }
1028
+ // </d>
1029
+
1030
+ if (!oScope) {
1031
+ oScope = _win;
1032
+ }
1033
+
1034
+ _addOnEvent(sType, oMethod, oScope);
1035
+ _processOnEvents();
1036
+
1037
+ return true;
1038
+
1039
+ } else {
1040
+
1041
+ throw _str('needFunction', sType);
1042
+
1043
+ }
1044
+
1045
+ };
1046
+
1047
+ /**
1048
+ * Queues a callback for execution when SoundManager has failed to initialize.
1049
+ *
1050
+ * @param {function} oMethod The callback method to fire
1051
+ * @param {object} oScope Optional: The scope to apply to the callback
1052
+ */
1053
+
1054
+ this.ontimeout = function(oMethod, oScope) {
1055
+
1056
+ var sType = 'ontimeout';
1057
+
1058
+ if (oMethod && oMethod instanceof Function) {
1059
+
1060
+ // <d>
1061
+ if (_didInit) {
1062
+ _s._wD(_str('queue', sType));
1063
+ }
1064
+ // </d>
1065
+
1066
+ if (!oScope) {
1067
+ oScope = _win;
1068
+ }
1069
+
1070
+ _addOnEvent(sType, oMethod, oScope);
1071
+ _processOnEvents({type:sType});
1072
+
1073
+ return true;
1074
+
1075
+ } else {
1076
+
1077
+ throw _str('needFunction', sType);
1078
+
1079
+ }
1080
+
1081
+ };
1082
+
1083
+ /**
1084
+ * Writes console.log()-style debug output to a console or in-browser element.
1085
+ * Applies when SoundManager.debugMode = true
1086
+ *
1087
+ * @param {string} sText The console message
1088
+ * @param {string} sType Optional: Log type of 'info', 'warn' or 'error'
1089
+ * @param {object} Optional: The scope to apply to the callback
1090
+ */
1091
+
1092
+ this._writeDebug = function(sText, sType, _bTimestamp) {
1093
+
1094
+ // pseudo-private console.log()-style output
1095
+ // <d>
1096
+
1097
+ var sDID = 'soundmanager-debug', o, oItem, sMethod;
1098
+
1099
+ if (!_s.debugMode) {
1100
+ return false;
1101
+ }
1102
+
1103
+ if (typeof _bTimestamp !== 'undefined' && _bTimestamp) {
1104
+ sText = sText + ' | ' + new Date().getTime();
1105
+ }
1106
+
1107
+ if (_hasConsole && _s.useConsole) {
1108
+ sMethod = _debugLevels[sType];
1109
+ if (typeof console[sMethod] !== 'undefined') {
1110
+ console[sMethod](sText);
1111
+ } else {
1112
+ console.log(sText);
1113
+ }
1114
+ if (_s.consoleOnly) {
1115
+ return true;
1116
+ }
1117
+ }
1118
+
1119
+ try {
1120
+
1121
+ o = _id(sDID);
1122
+
1123
+ if (!o) {
1124
+ return false;
1125
+ }
1126
+
1127
+ oItem = _doc.createElement('div');
1128
+
1129
+ if (++_wdCount % 2 === 0) {
1130
+ oItem.className = 'sm2-alt';
1131
+ }
1132
+
1133
+ if (typeof sType === 'undefined') {
1134
+ sType = 0;
1135
+ } else {
1136
+ sType = parseInt(sType, 10);
1137
+ }
1138
+
1139
+ oItem.appendChild(_doc.createTextNode(sText));
1140
+
1141
+ if (sType) {
1142
+ if (sType >= 2) {
1143
+ oItem.style.fontWeight = 'bold';
1144
+ }
1145
+ if (sType === 3) {
1146
+ oItem.style.color = '#ff3333';
1147
+ }
1148
+ }
1149
+
1150
+ // top-to-bottom
1151
+ // o.appendChild(oItem);
1152
+
1153
+ // bottom-to-top
1154
+ o.insertBefore(oItem, o.firstChild);
1155
+
1156
+ } catch(e) {
1157
+ // oh well
1158
+ }
1159
+
1160
+ o = null;
1161
+ // </d>
1162
+
1163
+ return true;
1164
+
1165
+ };
1166
+
1167
+ // alias
1168
+ this._wD = this._writeDebug;
1169
+
1170
+ /**
1171
+ * Provides debug / state information on all SMSound objects.
1172
+ */
1173
+
1174
+ this._debug = function() {
1175
+
1176
+ // <d>
1177
+ var i, j;
1178
+ _wDS('currentObj', 1);
1179
+
1180
+ for (i = 0, j = _s.soundIDs.length; i < j; i++) {
1181
+ _s.sounds[_s.soundIDs[i]]._debug();
1182
+ }
1183
+ // </d>
1184
+
1185
+ };
1186
+
1187
+ /**
1188
+ * Restarts and re-initializes the SoundManager instance.
1189
+ */
1190
+
1191
+ this.reboot = function() {
1192
+
1193
+ // attempt to reset and init SM2
1194
+ _s._wD(_sm+'.reboot()');
1195
+
1196
+ // <d>
1197
+ if (_s.soundIDs.length) {
1198
+ _s._wD('Destroying ' + _s.soundIDs.length + ' SMSound objects...');
1199
+ }
1200
+ // </d>
1201
+
1202
+ var i, j;
1203
+
1204
+ for (i = _s.soundIDs.length-1; i >= 0; i--) {
1205
+ _s.sounds[_s.soundIDs[i]].destruct();
1206
+ }
1207
+
1208
+ // trash ze flash
1209
+
1210
+ try {
1211
+ if (_isIE) {
1212
+ _oRemovedHTML = _flash.innerHTML;
1213
+ }
1214
+ _oRemoved = _flash.parentNode.removeChild(_flash);
1215
+ _s._wD('Flash movie removed.');
1216
+ } catch(e) {
1217
+ // uh-oh.
1218
+ _wDS('badRemove', 2);
1219
+ }
1220
+
1221
+ // actually, force recreate of movie.
1222
+ _oRemovedHTML = _oRemoved = _needsFlash = null;
1223
+
1224
+ _s.enabled = _didDCLoaded = _didInit = _waitingForEI = _initPending = _didAppend = _appendSuccess = _disabled = _s.swfLoaded = false;
1225
+ _s.soundIDs = [];
1226
+ _s.sounds = {};
1227
+ _flash = null;
1228
+
1229
+ for (i in _on_queue) {
1230
+ if (_on_queue.hasOwnProperty(i)) {
1231
+ for (j = _on_queue[i].length-1; j >= 0; j--) {
1232
+ _on_queue[i][j].fired = false;
1233
+ }
1234
+ }
1235
+ }
1236
+
1237
+ _s._wD(_sm + ': Rebooting...');
1238
+ _win.setTimeout(_s.beginDelayedInit, 20);
1239
+
1240
+ };
1241
+
1242
+ /**
1243
+ * Undocumented: Determines the SM2 flash movie's load progress.
1244
+ *
1245
+ * @return {number or null} Percent loaded, or if invalid/unsupported, null.
1246
+ */
1247
+
1248
+ this.getMoviePercent = function() {
1249
+
1250
+ return (_flash && typeof _flash.PercentLoaded !== 'undefined' ? _flash.PercentLoaded() : null);
1251
+
1252
+ };
1253
+
1254
+ /**
1255
+ * Additional helper for manually invoking SM2's init process after DOM Ready / window.onload().
1256
+ */
1257
+
1258
+ this.beginDelayedInit = function() {
1259
+
1260
+ _windowLoaded = true;
1261
+ _domContentLoaded();
1262
+
1263
+ setTimeout(function() {
1264
+
1265
+ if (_initPending) {
1266
+ return false;
1267
+ }
1268
+
1269
+ _createMovie();
1270
+ _initMovie();
1271
+ _initPending = true;
1272
+
1273
+ return true;
1274
+
1275
+ }, 20);
1276
+
1277
+ _delayWaitForEI();
1278
+
1279
+ };
1280
+
1281
+ /**
1282
+ * Destroys the SoundManager instance and all SMSound instances.
1283
+ */
1284
+
1285
+ this.destruct = function() {
1286
+
1287
+ _s._wD(_sm+'.destruct()');
1288
+ _s.disable(true);
1289
+
1290
+ };
1291
+
1292
+ /**
1293
+ * SMSound() (sound object) constructor
1294
+ * ------------------------------------
1295
+ *
1296
+ * @param {object} oOptions Sound options (id and url are required attributes)
1297
+ * @return {SMSound} The new SMSound object
1298
+ */
1299
+
1300
+ SMSound = function(oOptions) {
1301
+
1302
+ var _t = this, _resetProperties, _add_html5_events, _remove_html5_events, _stop_html5_timer, _start_html5_timer, _attachOnPosition, _onplay_called = false, _onPositionItems = [], _onPositionFired = 0, _detachOnPosition, _applyFromTo, _lastURL = null, _lastHTML5State;
1303
+
1304
+ _lastHTML5State = {
1305
+ // tracks duration + position (time)
1306
+ duration: null,
1307
+ time: null
1308
+ };
1309
+
1310
+ this.sID = oOptions.id;
1311
+ this.url = oOptions.url;
1312
+ this.options = _mixin(oOptions);
1313
+
1314
+ // per-play-instance-specific options
1315
+ this.instanceOptions = this.options;
1316
+
1317
+ // short alias
1318
+ this._iO = this.instanceOptions;
1319
+
1320
+ // assign property defaults
1321
+ this.pan = this.options.pan;
1322
+ this.volume = this.options.volume;
1323
+ this.isHTML5 = false;
1324
+ this._a = null;
1325
+
1326
+ /**
1327
+ * SMSound() public methods
1328
+ * ------------------------
1329
+ */
1330
+
1331
+ this.id3 = {};
1332
+
1333
+ /**
1334
+ * Writes SMSound object parameters to debug console
1335
+ */
1336
+
1337
+ this._debug = function() {
1338
+
1339
+ // <d>
1340
+ // pseudo-private console.log()-style output
1341
+
1342
+ if (_s.debugMode) {
1343
+
1344
+ var stuff = null, msg = [], sF, sfBracket, maxLength = 64;
1345
+
1346
+ for (stuff in _t.options) {
1347
+ if (_t.options[stuff] !== null) {
1348
+ if (_t.options[stuff] instanceof Function) {
1349
+ // handle functions specially
1350
+ sF = _t.options[stuff].toString();
1351
+ // normalize spaces
1352
+ sF = sF.replace(/\s\s+/g, ' ');
1353
+ sfBracket = sF.indexOf('{');
1354
+ msg.push(' ' + stuff + ': {' + sF.substr(sfBracket + 1, (Math.min(Math.max(sF.indexOf('\n') - 1, maxLength), maxLength))).replace(/\n/g, '') + '... }');
1355
+ } else {
1356
+ msg.push(' ' + stuff + ': ' + _t.options[stuff]);
1357
+ }
1358
+ }
1359
+ }
1360
+
1361
+ _s._wD('SMSound() merged options: {\n' + msg.join(', \n') + '\n}');
1362
+
1363
+ }
1364
+ // </d>
1365
+
1366
+ };
1367
+
1368
+ // <d>
1369
+ this._debug();
1370
+ // </d>
1371
+
1372
+ /**
1373
+ * Begins loading a sound per its *url*.
1374
+ *
1375
+ * @param {object} oOptions Optional: Sound options
1376
+ * @return {SMSound} The SMSound object
1377
+ */
1378
+
1379
+ this.load = function(oOptions) {
1380
+
1381
+ var oS = null, _iO;
1382
+
1383
+ if (typeof oOptions !== 'undefined') {
1384
+ _t._iO = _mixin(oOptions, _t.options);
1385
+ _t.instanceOptions = _t._iO;
1386
+ } else {
1387
+ oOptions = _t.options;
1388
+ _t._iO = oOptions;
1389
+ _t.instanceOptions = _t._iO;
1390
+ if (_lastURL && _lastURL !== _t.url) {
1391
+ _wDS('manURL');
1392
+ _t._iO.url = _t.url;
1393
+ _t.url = null;
1394
+ }
1395
+ }
1396
+
1397
+ if (!_t._iO.url) {
1398
+ _t._iO.url = _t.url;
1399
+ }
1400
+
1401
+ _t._iO.url = _parseURL(_t._iO.url);
1402
+
1403
+ _s._wD('SMSound.load(): ' + _t._iO.url, 1);
1404
+
1405
+ if (_t._iO.url === _t.url && _t.readyState !== 0 && _t.readyState !== 2) {
1406
+ _wDS('onURL', 1);
1407
+ // if loaded and an onload() exists, fire immediately.
1408
+ if (_t.readyState === 3 && _t._iO.onload) {
1409
+ // assume success based on truthy duration.
1410
+ _t._iO.onload.apply(_t, [(!!_t.duration)]);
1411
+ }
1412
+ return _t;
1413
+ }
1414
+
1415
+ // local shortcut
1416
+ _iO = _t._iO;
1417
+
1418
+ _lastURL = _t.url;
1419
+ _t.loaded = false;
1420
+ _t.readyState = 1;
1421
+ _t.playState = 0;
1422
+
1423
+ // TODO: If switching from HTML5 -> flash (or vice versa), stop currently-playing audio.
1424
+
1425
+ if (_html5OK(_iO)) {
1426
+
1427
+ oS = _t._setup_html5(_iO);
1428
+
1429
+ if (!oS._called_load) {
1430
+
1431
+ _s._wD(_h5+'load: '+_t.sID);
1432
+ _t._html5_canplay = false;
1433
+
1434
+ // given explicit load call, try to get whole file.
1435
+ // early HTML5 implementation (non-standard)
1436
+ _t._a.autobuffer = 'auto';
1437
+
1438
+ // standard
1439
+ _t._a.preload = 'auto';
1440
+
1441
+ oS.load();
1442
+ oS._called_load = true;
1443
+
1444
+ if (_iO.autoPlay) {
1445
+ _t.play();
1446
+ }
1447
+
1448
+ } else {
1449
+ _s._wD(_h5+'ignoring request to load again: '+_t.sID);
1450
+ }
1451
+
1452
+ } else {
1453
+
1454
+ try {
1455
+ _t.isHTML5 = false;
1456
+ _t._iO = _policyFix(_loopFix(_iO));
1457
+ // re-assign local shortcut
1458
+ _iO = _t._iO;
1459
+ if (_fV === 8) {
1460
+ _flash._load(_t.sID, _iO.url, _iO.stream, _iO.autoPlay, (_iO.whileloading?1:0), _iO.loops||1, _iO.usePolicyFile);
1461
+ } else {
1462
+ _flash._load(_t.sID, _iO.url, !!(_iO.stream), !!(_iO.autoPlay), _iO.loops||1, !!(_iO.autoLoad), _iO.usePolicyFile);
1463
+ }
1464
+ } catch(e) {
1465
+ _wDS('smError', 2);
1466
+ _debugTS('onload', false);
1467
+ _catchError({type:'SMSOUND_LOAD_JS_EXCEPTION', fatal:true});
1468
+
1469
+ }
1470
+
1471
+ }
1472
+
1473
+ return _t;
1474
+
1475
+ };
1476
+
1477
+ /**
1478
+ * Unloads a sound, canceling any open HTTP requests.
1479
+ *
1480
+ * @return {SMSound} The SMSound object
1481
+ */
1482
+
1483
+ this.unload = function() {
1484
+
1485
+ // Flash 8/AS2 can't "close" a stream - fake it by loading an empty URL
1486
+ // Flash 9/AS3: Close stream, preventing further load
1487
+ // HTML5: Most UAs will use empty URL
1488
+
1489
+ if (_t.readyState !== 0) {
1490
+
1491
+ _s._wD('SMSound.unload(): "' + _t.sID + '"');
1492
+
1493
+ if (!_t.isHTML5) {
1494
+ if (_fV === 8) {
1495
+ _flash._unload(_t.sID, _emptyURL);
1496
+ } else {
1497
+ _flash._unload(_t.sID);
1498
+ }
1499
+ } else {
1500
+ _stop_html5_timer();
1501
+ if (_t._a) {
1502
+ _t._a.pause();
1503
+ _html5Unload(_t._a);
1504
+ }
1505
+ }
1506
+
1507
+ // reset load/status flags
1508
+ _resetProperties();
1509
+
1510
+ }
1511
+
1512
+ return _t;
1513
+
1514
+ };
1515
+
1516
+ /**
1517
+ * Unloads and destroys a sound.
1518
+ */
1519
+
1520
+ this.destruct = function(_bFromSM) {
1521
+
1522
+ _s._wD('SMSound.destruct(): "' + _t.sID + '"');
1523
+
1524
+ if (!_t.isHTML5) {
1525
+
1526
+ // kill sound within Flash
1527
+ // Disable the onfailure handler
1528
+ _t._iO.onfailure = null;
1529
+ _flash._destroySound(_t.sID);
1530
+
1531
+ } else {
1532
+
1533
+ _stop_html5_timer();
1534
+
1535
+ if (_t._a) {
1536
+ _t._a.pause();
1537
+ _html5Unload(_t._a);
1538
+ if (!_useGlobalHTML5Audio) {
1539
+ _remove_html5_events();
1540
+ }
1541
+ // break obvious circular reference
1542
+ _t._a._t = null;
1543
+ _t._a = null;
1544
+ }
1545
+
1546
+ }
1547
+
1548
+ if (!_bFromSM) {
1549
+ // ensure deletion from controller
1550
+ _s.destroySound(_t.sID, true);
1551
+
1552
+ }
1553
+
1554
+ };
1555
+
1556
+ /**
1557
+ * Begins playing a sound.
1558
+ *
1559
+ * @param {object} oOptions Optional: Sound options
1560
+ * @return {SMSound} The SMSound object
1561
+ */
1562
+
1563
+ this.play = function(oOptions, _updatePlayState) {
1564
+
1565
+ var fN, allowMulti, a, onready;
1566
+
1567
+ // <d>
1568
+ fN = 'SMSound.play(): ';
1569
+ // </d>
1570
+
1571
+ _updatePlayState = _updatePlayState === undefined ? true : _updatePlayState; // default to true
1572
+
1573
+ if (!oOptions) {
1574
+ oOptions = {};
1575
+ }
1576
+
1577
+ _t._iO = _mixin(oOptions, _t._iO);
1578
+ _t._iO = _mixin(_t._iO, _t.options);
1579
+ _t._iO.url = _parseURL(_t._iO.url);
1580
+ _t.instanceOptions = _t._iO;
1581
+
1582
+ // RTMP-only
1583
+ if (_t._iO.serverURL && !_t.connected) {
1584
+ if (!_t.getAutoPlay()) {
1585
+ _s._wD(fN+' Netstream not connected yet - setting autoPlay');
1586
+ _t.setAutoPlay(true);
1587
+ }
1588
+ // play will be called in _onconnect()
1589
+ return _t;
1590
+ }
1591
+
1592
+ if (_html5OK(_t._iO)) {
1593
+ _t._setup_html5(_t._iO);
1594
+ _start_html5_timer();
1595
+ }
1596
+
1597
+ if (_t.playState === 1 && !_t.paused) {
1598
+ allowMulti = _t._iO.multiShot;
1599
+ if (!allowMulti) {
1600
+ _s._wD(fN + '"' + _t.sID + '" already playing (one-shot)', 1);
1601
+ return _t;
1602
+ } else {
1603
+ _s._wD(fN + '"' + _t.sID + '" already playing (multi-shot)', 1);
1604
+ }
1605
+ }
1606
+
1607
+ if (!_t.loaded) {
1608
+
1609
+ if (_t.readyState === 0) {
1610
+
1611
+ _s._wD(fN + 'Attempting to load "' + _t.sID + '"', 1);
1612
+
1613
+ // try to get this sound playing ASAP
1614
+ if (!_t.isHTML5) {
1615
+ // assign directly because setAutoPlay() increments the instanceCount
1616
+ _t._iO.autoPlay = true;
1617
+ }
1618
+
1619
+ _t.load(_t._iO);
1620
+
1621
+ } else if (_t.readyState === 2) {
1622
+
1623
+ _s._wD(fN + 'Could not load "' + _t.sID + '" - exiting', 2);
1624
+ return _t;
1625
+
1626
+ } else {
1627
+
1628
+ _s._wD(fN + '"' + _t.sID + '" is loading - attempting to play..', 1);
1629
+
1630
+ }
1631
+
1632
+ } else {
1633
+
1634
+ _s._wD(fN + '"' + _t.sID + '"');
1635
+
1636
+ }
1637
+
1638
+ if (!_t.isHTML5 && _fV === 9 && _t.position > 0 && _t.position === _t.duration) {
1639
+ // flash 9 needs a position reset if play() is called while at the end of a sound.
1640
+ _s._wD(fN + '"' + _t.sID + '": Sound at end, resetting to position:0');
1641
+ oOptions.position = 0;
1642
+ }
1643
+
1644
+ /**
1645
+ * Streams will pause when their buffer is full if they are being loaded.
1646
+ * In this case paused is true, but the song hasn't started playing yet.
1647
+ * If we just call resume() the onplay() callback will never be called.
1648
+ * So only call resume() if the position is > 0.
1649
+ * Another reason is because options like volume won't have been applied yet.
1650
+ */
1651
+
1652
+ if (_t.paused && _t.position && _t.position > 0) {
1653
+
1654
+ // https://gist.github.com/37b17df75cc4d7a90bf6
1655
+ _s._wD(fN + '"' + _t.sID + '" is resuming from paused state',1);
1656
+ _t.resume();
1657
+
1658
+ } else {
1659
+
1660
+ _t._iO = _mixin(oOptions, _t._iO);
1661
+
1662
+ // apply from/to parameters, if they exist (and not using RTMP)
1663
+ if (_t._iO.from !== null && _t._iO.to !== null && _t.instanceCount === 0 && _t.playState === 0 && !_t._iO.serverURL) {
1664
+
1665
+ onready = function() {
1666
+ // sound "canplay" or onload()
1667
+ // re-apply from/to to instance options, and start playback
1668
+ _t._iO = _mixin(oOptions, _t._iO);
1669
+ _t.play(_t._iO);
1670
+ };
1671
+
1672
+ // HTML5 needs to at least have "canplay" fired before seeking.
1673
+ if (_t.isHTML5 && !_t._html5_canplay) {
1674
+
1675
+ // this hasn't been loaded yet. load it first, and then do this again.
1676
+ _s._wD(fN+'Beginning load of "'+ _t.sID+'" for from/to case');
1677
+
1678
+ _t.load({
1679
+ _oncanplay: onready
1680
+ });
1681
+
1682
+ return false;
1683
+
1684
+ } else if (!_t.isHTML5 && !_t.loaded && (!_t.readyState || _t.readyState !== 2)) {
1685
+
1686
+ // to be safe, preload the whole thing in Flash.
1687
+
1688
+ _s._wD(fN+'Preloading "'+ _t.sID+'" for from/to case');
1689
+
1690
+ _t.load({
1691
+ onload: onready
1692
+ });
1693
+
1694
+ return false;
1695
+
1696
+ }
1697
+
1698
+ // otherwise, we're ready to go. re-apply local options, and continue
1699
+
1700
+ _t._iO = _applyFromTo();
1701
+
1702
+ }
1703
+
1704
+ _s._wD(fN+'"'+ _t.sID+'" is starting to play');
1705
+
1706
+ if (!_t.instanceCount || _t._iO.multiShotEvents || (!_t.isHTML5 && _fV > 8 && !_t.getAutoPlay())) {
1707
+ _t.instanceCount++;
1708
+ }
1709
+
1710
+ // if first play and onposition parameters exist, apply them now
1711
+ if (_t.playState === 0 && _t._iO.onposition) {
1712
+ _attachOnPosition(_t);
1713
+ }
1714
+
1715
+ _t.playState = 1;
1716
+ _t.paused = false;
1717
+
1718
+ _t.position = (typeof _t._iO.position !== 'undefined' && !isNaN(_t._iO.position) ? _t._iO.position : 0);
1719
+
1720
+ if (!_t.isHTML5) {
1721
+ _t._iO = _policyFix(_loopFix(_t._iO));
1722
+ }
1723
+
1724
+ if (_t._iO.onplay && _updatePlayState) {
1725
+ _t._iO.onplay.apply(_t);
1726
+ _onplay_called = true;
1727
+ }
1728
+
1729
+ _t.setVolume(_t._iO.volume, true);
1730
+ _t.setPan(_t._iO.pan, true);
1731
+
1732
+ if (!_t.isHTML5) {
1733
+
1734
+ _flash._start(_t.sID, _t._iO.loops || 1, (_fV === 9?_t._iO.position:_t._iO.position / 1000));
1735
+
1736
+ } else {
1737
+
1738
+ _start_html5_timer();
1739
+ a = _t._setup_html5();
1740
+ _t.setPosition(_t._iO.position);
1741
+ a.play();
1742
+
1743
+ }
1744
+
1745
+ }
1746
+
1747
+ return _t;
1748
+
1749
+ };
1750
+
1751
+ // just for convenience
1752
+ this.start = this.play;
1753
+
1754
+ /**
1755
+ * Stops playing a sound (and optionally, all sounds)
1756
+ *
1757
+ * @param {boolean} bAll Optional: Whether to stop all sounds
1758
+ * @return {SMSound} The SMSound object
1759
+ */
1760
+
1761
+ this.stop = function(bAll) {
1762
+
1763
+ var _iO = _t._iO, _oP;
1764
+
1765
+ if (_t.playState === 1) {
1766
+
1767
+ _t._onbufferchange(0);
1768
+ _t._resetOnPosition(0);
1769
+ _t.paused = false;
1770
+
1771
+ if (!_t.isHTML5) {
1772
+ _t.playState = 0;
1773
+ }
1774
+
1775
+ // remove onPosition listeners, if any
1776
+ _detachOnPosition();
1777
+
1778
+ // and "to" position, if set
1779
+ if (_iO.to) {
1780
+ _t.clearOnPosition(_iO.to);
1781
+ }
1782
+
1783
+ if (!_t.isHTML5) {
1784
+
1785
+ _flash._stop(_t.sID, bAll);
1786
+
1787
+ // hack for netStream: just unload
1788
+ if (_iO.serverURL) {
1789
+ _t.unload();
1790
+ }
1791
+
1792
+ } else {
1793
+
1794
+ if (_t._a) {
1795
+
1796
+ _oP = _t.position;
1797
+
1798
+ // act like Flash, though
1799
+ _t.setPosition(0);
1800
+
1801
+ // hack: reflect old position for onstop() (also like Flash)
1802
+ _t.position = _oP;
1803
+
1804
+ // html5 has no stop()
1805
+ // NOTE: pausing means iOS requires interaction to resume.
1806
+ _t._a.pause();
1807
+
1808
+ _t.playState = 0;
1809
+
1810
+ // and update UI
1811
+ _t._onTimer();
1812
+
1813
+ _stop_html5_timer();
1814
+
1815
+ }
1816
+
1817
+ }
1818
+
1819
+ _t.instanceCount = 0;
1820
+ _t._iO = {};
1821
+
1822
+ if (_iO.onstop) {
1823
+ _iO.onstop.apply(_t);
1824
+ }
1825
+
1826
+ }
1827
+
1828
+ return _t;
1829
+
1830
+ };
1831
+
1832
+ /**
1833
+ * Undocumented/internal: Sets autoPlay for RTMP.
1834
+ *
1835
+ * @param {boolean} autoPlay state
1836
+ */
1837
+
1838
+ this.setAutoPlay = function(autoPlay) {
1839
+
1840
+ _s._wD('sound '+_t.sID+' turned autoplay ' + (autoPlay ? 'on' : 'off'));
1841
+ _t._iO.autoPlay = autoPlay;
1842
+
1843
+ if (!_t.isHTML5) {
1844
+ _flash._setAutoPlay(_t.sID, autoPlay);
1845
+ if (autoPlay) {
1846
+ // only increment the instanceCount if the sound isn't loaded (TODO: verify RTMP)
1847
+ if (!_t.instanceCount && _t.readyState === 1) {
1848
+ _t.instanceCount++;
1849
+ _s._wD('sound '+_t.sID+' incremented instance count to '+_t.instanceCount);
1850
+ }
1851
+ }
1852
+ }
1853
+
1854
+ };
1855
+
1856
+ /**
1857
+ * Undocumented/internal: Returns the autoPlay boolean.
1858
+ *
1859
+ * @return {boolean} The current autoPlay value
1860
+ */
1861
+
1862
+ this.getAutoPlay = function() {
1863
+
1864
+ return _t._iO.autoPlay;
1865
+
1866
+ };
1867
+
1868
+ /**
1869
+ * Sets the position of a sound.
1870
+ *
1871
+ * @param {number} nMsecOffset Position (milliseconds)
1872
+ * @return {SMSound} The SMSound object
1873
+ */
1874
+
1875
+ this.setPosition = function(nMsecOffset) {
1876
+
1877
+ if (nMsecOffset === undefined) {
1878
+ nMsecOffset = 0;
1879
+ }
1880
+
1881
+ var original_pos,
1882
+ position, position1K,
1883
+ // Use the duration from the instance options, if we don't have a track duration yet.
1884
+ // position >= 0 and <= current available (loaded) duration
1885
+ offset = (_t.isHTML5 ? Math.max(nMsecOffset,0) : Math.min(_t.duration || _t._iO.duration, Math.max(nMsecOffset, 0)));
1886
+
1887
+ original_pos = _t.position;
1888
+ _t.position = offset;
1889
+ position1K = _t.position/1000;
1890
+ _t._resetOnPosition(_t.position);
1891
+ _t._iO.position = offset;
1892
+
1893
+ if (!_t.isHTML5) {
1894
+
1895
+ position = (_fV === 9 ? _t.position : position1K);
1896
+ if (_t.readyState && _t.readyState !== 2) {
1897
+ // if paused or not playing, will not resume (by playing)
1898
+ _flash._setPosition(_t.sID, position, (_t.paused || !_t.playState));
1899
+ }
1900
+
1901
+ } else if (_t._a) {
1902
+
1903
+ // Set the position in the canplay handler if the sound is not ready yet
1904
+ if (_t._html5_canplay) {
1905
+ if (_t._a.currentTime !== position1K) {
1906
+ /**
1907
+ * DOM/JS errors/exceptions to watch out for:
1908
+ * if seek is beyond (loaded?) position, "DOM exception 11"
1909
+ * "INDEX_SIZE_ERR": DOM exception 1
1910
+ */
1911
+ _s._wD('setPosition('+position1K+'): setting position');
1912
+ try {
1913
+ _t._a.currentTime = position1K;
1914
+ if (_t.playState === 0 || _t.paused) {
1915
+ // allow seek without auto-play/resume
1916
+ _t._a.pause();
1917
+ }
1918
+ } catch(e) {
1919
+ _s._wD('setPosition('+position1K+'): setting position failed: '+e.message, 2);
1920
+ }
1921
+ }
1922
+ } else {
1923
+ _s._wD('setPosition('+position1K+'): delaying, sound not ready');
1924
+ }
1925
+
1926
+ }
1927
+
1928
+ if (_t.isHTML5) {
1929
+ if (_t.paused) {
1930
+ // if paused, refresh UI right away
1931
+ // force update
1932
+ _t._onTimer(true);
1933
+ }
1934
+ }
1935
+
1936
+ return _t;
1937
+
1938
+ };
1939
+
1940
+ /**
1941
+ * Pauses sound playback.
1942
+ *
1943
+ * @return {SMSound} The SMSound object
1944
+ */
1945
+
1946
+ this.pause = function(_bCallFlash) {
1947
+
1948
+ if (_t.paused || (_t.playState === 0 && _t.readyState !== 1)) {
1949
+ return _t;
1950
+ }
1951
+
1952
+ _s._wD('SMSound.pause()');
1953
+ _t.paused = true;
1954
+
1955
+ if (!_t.isHTML5) {
1956
+ if (_bCallFlash || _bCallFlash === undefined) {
1957
+ _flash._pause(_t.sID);
1958
+ }
1959
+ } else {
1960
+ _t._setup_html5().pause();
1961
+ _stop_html5_timer();
1962
+ }
1963
+
1964
+ if (_t._iO.onpause) {
1965
+ _t._iO.onpause.apply(_t);
1966
+ }
1967
+
1968
+ return _t;
1969
+
1970
+ };
1971
+
1972
+ /**
1973
+ * Resumes sound playback.
1974
+ *
1975
+ * @return {SMSound} The SMSound object
1976
+ */
1977
+
1978
+ /**
1979
+ * When auto-loaded streams pause on buffer full they have a playState of 0.
1980
+ * We need to make sure that the playState is set to 1 when these streams "resume".
1981
+ * When a paused stream is resumed, we need to trigger the onplay() callback if it
1982
+ * hasn't been called already. In this case since the sound is being played for the
1983
+ * first time, I think it's more appropriate to call onplay() rather than onresume().
1984
+ */
1985
+
1986
+ this.resume = function() {
1987
+
1988
+ var _iO = _t._iO;
1989
+
1990
+ if (!_t.paused) {
1991
+ return _t;
1992
+ }
1993
+
1994
+ _s._wD('SMSound.resume()');
1995
+ _t.paused = false;
1996
+ _t.playState = 1;
1997
+
1998
+ if (!_t.isHTML5) {
1999
+ if (_iO.isMovieStar && !_iO.serverURL) {
2000
+ // Bizarre Webkit bug (Chrome reported via 8tracks.com dudes): AAC content paused for 30+ seconds(?) will not resume without a reposition.
2001
+ _t.setPosition(_t.position);
2002
+ }
2003
+ // flash method is toggle-based (pause/resume)
2004
+ _flash._pause(_t.sID);
2005
+ } else {
2006
+ _t._setup_html5().play();
2007
+ _start_html5_timer();
2008
+ }
2009
+
2010
+ if (!_onplay_called && _iO.onplay) {
2011
+ _iO.onplay.apply(_t);
2012
+ _onplay_called = true;
2013
+ } else if (_iO.onresume) {
2014
+ _iO.onresume.apply(_t);
2015
+ }
2016
+
2017
+ return _t;
2018
+
2019
+ };
2020
+
2021
+ /**
2022
+ * Toggles sound playback.
2023
+ *
2024
+ * @return {SMSound} The SMSound object
2025
+ */
2026
+
2027
+ this.togglePause = function() {
2028
+
2029
+ _s._wD('SMSound.togglePause()');
2030
+
2031
+ if (_t.playState === 0) {
2032
+ _t.play({
2033
+ position: (_fV === 9 && !_t.isHTML5 ? _t.position : _t.position / 1000)
2034
+ });
2035
+ return _t;
2036
+ }
2037
+
2038
+ if (_t.paused) {
2039
+ _t.resume();
2040
+ } else {
2041
+ _t.pause();
2042
+ }
2043
+
2044
+ return _t;
2045
+
2046
+ };
2047
+
2048
+ /**
2049
+ * Sets the panning (L-R) effect.
2050
+ *
2051
+ * @param {number} nPan The pan value (-100 to 100)
2052
+ * @return {SMSound} The SMSound object
2053
+ */
2054
+
2055
+ this.setPan = function(nPan, bInstanceOnly) {
2056
+
2057
+ if (typeof nPan === 'undefined') {
2058
+ nPan = 0;
2059
+ }
2060
+
2061
+ if (typeof bInstanceOnly === 'undefined') {
2062
+ bInstanceOnly = false;
2063
+ }
2064
+
2065
+ if (!_t.isHTML5) {
2066
+ _flash._setPan(_t.sID, nPan);
2067
+ } // else { no HTML5 pan? }
2068
+
2069
+ _t._iO.pan = nPan;
2070
+
2071
+ if (!bInstanceOnly) {
2072
+ _t.pan = nPan;
2073
+ _t.options.pan = nPan;
2074
+ }
2075
+
2076
+ return _t;
2077
+
2078
+ };
2079
+
2080
+ /**
2081
+ * Sets the volume.
2082
+ *
2083
+ * @param {number} nVol The volume value (0 to 100)
2084
+ * @return {SMSound} The SMSound object
2085
+ */
2086
+
2087
+ this.setVolume = function(nVol, _bInstanceOnly) {
2088
+
2089
+ /**
2090
+ * Note: Setting volume has no effect on iOS "special snowflake" devices.
2091
+ * Hardware volume control overrides software, and volume
2092
+ * will always return 1 per Apple docs. (iOS 4 + 5.)
2093
+ * http://developer.apple.com/library/safari/documentation/AudioVideo/Conceptual/HTML-canvas-guide/AddingSoundtoCanvasAnimations/AddingSoundtoCanvasAnimations.html
2094
+ */
2095
+
2096
+ if (typeof nVol === 'undefined') {
2097
+ nVol = 100;
2098
+ }
2099
+
2100
+ if (typeof _bInstanceOnly === 'undefined') {
2101
+ _bInstanceOnly = false;
2102
+ }
2103
+
2104
+ if (!_t.isHTML5) {
2105
+ _flash._setVolume(_t.sID, (_s.muted && !_t.muted) || _t.muted?0:nVol);
2106
+ } else if (_t._a) {
2107
+ // valid range: 0-1
2108
+ _t._a.volume = Math.max(0, Math.min(1, nVol/100));
2109
+ }
2110
+
2111
+ _t._iO.volume = nVol;
2112
+
2113
+ if (!_bInstanceOnly) {
2114
+ _t.volume = nVol;
2115
+ _t.options.volume = nVol;
2116
+ }
2117
+
2118
+ return _t;
2119
+
2120
+ };
2121
+
2122
+ /**
2123
+ * Mutes the sound.
2124
+ *
2125
+ * @return {SMSound} The SMSound object
2126
+ */
2127
+
2128
+ this.mute = function() {
2129
+
2130
+ _t.muted = true;
2131
+
2132
+ if (!_t.isHTML5) {
2133
+ _flash._setVolume(_t.sID, 0);
2134
+ } else if (_t._a) {
2135
+ _t._a.muted = true;
2136
+ }
2137
+
2138
+ return _t;
2139
+
2140
+ };
2141
+
2142
+ /**
2143
+ * Unmutes the sound.
2144
+ *
2145
+ * @return {SMSound} The SMSound object
2146
+ */
2147
+
2148
+ this.unmute = function() {
2149
+
2150
+ _t.muted = false;
2151
+ var hasIO = typeof _t._iO.volume !== 'undefined';
2152
+
2153
+ if (!_t.isHTML5) {
2154
+ _flash._setVolume(_t.sID, hasIO?_t._iO.volume:_t.options.volume);
2155
+ } else if (_t._a) {
2156
+ _t._a.muted = false;
2157
+ }
2158
+
2159
+ return _t;
2160
+
2161
+ };
2162
+
2163
+ /**
2164
+ * Toggles the muted state of a sound.
2165
+ *
2166
+ * @return {SMSound} The SMSound object
2167
+ */
2168
+
2169
+ this.toggleMute = function() {
2170
+
2171
+ return (_t.muted?_t.unmute():_t.mute());
2172
+
2173
+ };
2174
+
2175
+ /**
2176
+ * Registers a callback to be fired when a sound reaches a given position during playback.
2177
+ *
2178
+ * @param {number} nPosition The position to watch for
2179
+ * @param {function} oMethod The relevant callback to fire
2180
+ * @param {object} oScope Optional: The scope to apply the callback to
2181
+ * @return {SMSound} The SMSound object
2182
+ */
2183
+
2184
+ this.onPosition = function(nPosition, oMethod, oScope) {
2185
+
2186
+ // TODO: basic dupe checking?
2187
+
2188
+ _onPositionItems.push({
2189
+ position: parseInt(nPosition, 10),
2190
+ method: oMethod,
2191
+ scope: (typeof oScope !== 'undefined' ? oScope : _t),
2192
+ fired: false
2193
+ });
2194
+
2195
+ return _t;
2196
+
2197
+ };
2198
+
2199
+ // legacy/backwards-compability: lower-case method name
2200
+ this.onposition = this.onPosition;
2201
+
2202
+ /**
2203
+ * Removes registered callback(s) from a sound, by position and/or callback.
2204
+ *
2205
+ * @param {number} nPosition The position to clear callback(s) for
2206
+ * @param {function} oMethod Optional: Identify one callback to be removed when multiple listeners exist for one position
2207
+ * @return {SMSound} The SMSound object
2208
+ */
2209
+
2210
+ this.clearOnPosition = function(nPosition, oMethod) {
2211
+
2212
+ var i;
2213
+
2214
+ nPosition = parseInt(nPosition, 10);
2215
+
2216
+ if (isNaN(nPosition)) {
2217
+ // safety check
2218
+ return false;
2219
+ }
2220
+
2221
+ for (i=0; i < _onPositionItems.length; i++) {
2222
+
2223
+ if (nPosition === _onPositionItems[i].position) {
2224
+ // remove this item if no method was specified, or, if the method matches
2225
+ if (!oMethod || (oMethod === _onPositionItems[i].method)) {
2226
+ if (_onPositionItems[i].fired) {
2227
+ // decrement "fired" counter, too
2228
+ _onPositionFired--;
2229
+ }
2230
+ _onPositionItems.splice(i, 1);
2231
+ }
2232
+ }
2233
+
2234
+ }
2235
+
2236
+ };
2237
+
2238
+ this._processOnPosition = function() {
2239
+
2240
+ var i, item, j = _onPositionItems.length;
2241
+
2242
+ if (!j || !_t.playState || _onPositionFired >= j) {
2243
+ return false;
2244
+ }
2245
+
2246
+ for (i=j-1; i >= 0; i--) {
2247
+ item = _onPositionItems[i];
2248
+ if (!item.fired && _t.position >= item.position) {
2249
+ item.fired = true;
2250
+ _onPositionFired++;
2251
+ item.method.apply(item.scope, [item.position]);
2252
+ }
2253
+ }
2254
+
2255
+ return true;
2256
+
2257
+ };
2258
+
2259
+ this._resetOnPosition = function(nPosition) {
2260
+
2261
+ // reset "fired" for items interested in this position
2262
+ var i, item, j = _onPositionItems.length;
2263
+
2264
+ if (!j) {
2265
+ return false;
2266
+ }
2267
+
2268
+ for (i=j-1; i >= 0; i--) {
2269
+ item = _onPositionItems[i];
2270
+ if (item.fired && nPosition <= item.position) {
2271
+ item.fired = false;
2272
+ _onPositionFired--;
2273
+ }
2274
+ }
2275
+
2276
+ return true;
2277
+
2278
+ };
2279
+
2280
+ /**
2281
+ * SMSound() private internals
2282
+ * --------------------------------
2283
+ */
2284
+
2285
+ _applyFromTo = function() {
2286
+
2287
+ var _iO = _t._iO,
2288
+ f = _iO.from,
2289
+ t = _iO.to,
2290
+ start, end;
2291
+
2292
+ end = function() {
2293
+
2294
+ // end has been reached.
2295
+ _s._wD(_t.sID + ': "to" time of ' + t + ' reached.');
2296
+
2297
+ // detach listener
2298
+ _t.clearOnPosition(t, end);
2299
+
2300
+ // stop should clear this, too
2301
+ _t.stop();
2302
+
2303
+ };
2304
+
2305
+ start = function() {
2306
+
2307
+ _s._wD(_t.sID + ': playing "from" ' + f);
2308
+
2309
+ // add listener for end
2310
+ if (t !== null && !isNaN(t)) {
2311
+ _t.onPosition(t, end);
2312
+ }
2313
+
2314
+ };
2315
+
2316
+ if (f !== null && !isNaN(f)) {
2317
+
2318
+ // apply to instance options, guaranteeing correct start position.
2319
+ _iO.position = f;
2320
+
2321
+ // multiShot timing can't be tracked, so prevent that.
2322
+ _iO.multiShot = false;
2323
+
2324
+ start();
2325
+
2326
+ }
2327
+
2328
+ // return updated instanceOptions including starting position
2329
+ return _iO;
2330
+
2331
+ };
2332
+
2333
+ _attachOnPosition = function() {
2334
+
2335
+ var item,
2336
+ op = _t._iO.onposition;
2337
+
2338
+ // attach onposition things, if any, now.
2339
+
2340
+ if (op) {
2341
+
2342
+ for (item in op) {
2343
+ if (op.hasOwnProperty(item)) {
2344
+ _t.onPosition(parseInt(item, 10), op[item]);
2345
+ }
2346
+ }
2347
+
2348
+ }
2349
+
2350
+ };
2351
+
2352
+ _detachOnPosition = function() {
2353
+
2354
+ var item,
2355
+ op = _t._iO.onposition;
2356
+
2357
+ // detach any onposition()-style listeners.
2358
+
2359
+ if (op) {
2360
+
2361
+ for (item in op) {
2362
+ if (op.hasOwnProperty(item)) {
2363
+ _t.clearOnPosition(parseInt(item, 10));
2364
+ }
2365
+ }
2366
+
2367
+ }
2368
+
2369
+ };
2370
+
2371
+ _start_html5_timer = function() {
2372
+
2373
+ if (_t.isHTML5) {
2374
+ _startTimer(_t);
2375
+ }
2376
+
2377
+ };
2378
+
2379
+ _stop_html5_timer = function() {
2380
+
2381
+ if (_t.isHTML5) {
2382
+ _stopTimer(_t);
2383
+ }
2384
+
2385
+ };
2386
+
2387
+ _resetProperties = function() {
2388
+
2389
+ _onPositionItems = [];
2390
+ _onPositionFired = 0;
2391
+ _onplay_called = false;
2392
+
2393
+ _t._hasTimer = null;
2394
+ _t._a = null;
2395
+ _t._html5_canplay = false;
2396
+ _t.bytesLoaded = null;
2397
+ _t.bytesTotal = null;
2398
+ _t.duration = (_t._iO && _t._iO.duration ? _t._iO.duration : null);
2399
+ _t.durationEstimate = null;
2400
+
2401
+ // legacy: 1D array
2402
+ _t.eqData = [];
2403
+
2404
+ _t.eqData.left = [];
2405
+ _t.eqData.right = [];
2406
+
2407
+ _t.failures = 0;
2408
+ _t.isBuffering = false;
2409
+ _t.instanceOptions = {};
2410
+ _t.instanceCount = 0;
2411
+ _t.loaded = false;
2412
+ _t.metadata = {};
2413
+
2414
+ // 0 = uninitialised, 1 = loading, 2 = failed/error, 3 = loaded/success
2415
+ _t.readyState = 0;
2416
+
2417
+ _t.muted = false;
2418
+ _t.paused = false;
2419
+
2420
+ _t.peakData = {
2421
+ left: 0,
2422
+ right: 0
2423
+ };
2424
+
2425
+ _t.waveformData = {
2426
+ left: [],
2427
+ right: []
2428
+ };
2429
+
2430
+ _t.playState = 0;
2431
+ _t.position = null;
2432
+
2433
+ };
2434
+
2435
+ _resetProperties();
2436
+
2437
+ /**
2438
+ * Pseudo-private SMSound internals
2439
+ * --------------------------------
2440
+ */
2441
+
2442
+ this._onTimer = function(bForce) {
2443
+
2444
+ /**
2445
+ * HTML5-only _whileplaying() etc.
2446
+ * called from both HTML5 native events, and polling/interval-based timers
2447
+ * mimics flash and fires only when time/duration change, so as to be polling-friendly
2448
+ */
2449
+
2450
+ var duration, isNew = false, time, x = {};
2451
+
2452
+ if (_t._hasTimer || bForce) {
2453
+
2454
+ // TODO: May not need to track readyState (1 = loading)
2455
+
2456
+ if (_t._a && (bForce || ((_t.playState > 0 || _t.readyState === 1) && !_t.paused))) {
2457
+
2458
+ duration = _t._get_html5_duration();
2459
+
2460
+ if (duration !== _lastHTML5State.duration) {
2461
+
2462
+ _lastHTML5State.duration = duration;
2463
+ _t.duration = duration;
2464
+ isNew = true;
2465
+
2466
+ }
2467
+
2468
+ // TODO: investigate why this goes wack if not set/re-set each time.
2469
+ _t.durationEstimate = _t.duration;
2470
+
2471
+ time = (_t._a.currentTime * 1000 || 0);
2472
+
2473
+ if (time !== _lastHTML5State.time) {
2474
+
2475
+ _lastHTML5State.time = time;
2476
+ isNew = true;
2477
+
2478
+ }
2479
+
2480
+ if (isNew || bForce) {
2481
+
2482
+ _t._whileplaying(time,x,x,x,x);
2483
+
2484
+ }
2485
+
2486
+ return isNew;
2487
+
2488
+ } else {
2489
+
2490
+ // _s._wD('_onTimer: Warn for "'+_t.sID+'": '+(!_t._a?'Could not find element. ':'')+(_t.playState === 0?'playState bad, 0?':'playState = '+_t.playState+', OK'));
2491
+
2492
+ return false;
2493
+
2494
+ }
2495
+
2496
+ }
2497
+
2498
+ };
2499
+
2500
+ this._get_html5_duration = function() {
2501
+
2502
+ var _iO = _t._iO,
2503
+ d = (_t._a ? _t._a.duration*1000 : (_iO ? _iO.duration : undefined)),
2504
+ result = (d && !isNaN(d) && d !== Infinity ? d : (_iO ? _iO.duration : null));
2505
+
2506
+ return result;
2507
+
2508
+ };
2509
+
2510
+ this._setup_html5 = function(oOptions) {
2511
+
2512
+ var _iO = _mixin(_t._iO, oOptions), d = decodeURI,
2513
+ _a = _useGlobalHTML5Audio ? _s._global_a : _t._a,
2514
+ _dURL = d(_iO.url),
2515
+ _oldIO = (_a && _a._t ? _a._t.instanceOptions : null);
2516
+
2517
+ if (_a) {
2518
+
2519
+ if (_a._t) {
2520
+
2521
+ if (!_useGlobalHTML5Audio && _dURL === d(_lastURL)) {
2522
+ // same url, ignore request
2523
+ return _a;
2524
+ } else if (_useGlobalHTML5Audio && _oldIO.url === _iO.url && (!_lastURL || (_lastURL === _oldIO.url))) {
2525
+ // iOS-type reuse case
2526
+ return _a;
2527
+ }
2528
+
2529
+ }
2530
+
2531
+ _s._wD('setting new URL on existing object: ' + _dURL + (_lastURL ? ', old URL: ' + _lastURL : ''));
2532
+
2533
+ /**
2534
+ * "First things first, I, Poppa.." (reset the previous state of the old sound, if playing)
2535
+ * Fixes case with devices that can only play one sound at a time
2536
+ * Otherwise, other sounds in mid-play will be terminated without warning and in a stuck state
2537
+ */
2538
+
2539
+ if (_useGlobalHTML5Audio && _a._t && _a._t.playState && _iO.url !== _oldIO.url) {
2540
+ _a._t.stop();
2541
+ }
2542
+
2543
+ // new URL, so reset load/playstate and so on
2544
+ _resetProperties();
2545
+
2546
+ _a.src = _iO.url;
2547
+ _t.url = _iO.url;
2548
+ _lastURL = _iO.url;
2549
+ _a._called_load = false;
2550
+
2551
+ } else {
2552
+
2553
+ _s._wD('creating HTML5 Audio() element with URL: '+_dURL);
2554
+ _a = new Audio(_iO.url);
2555
+
2556
+ _a._called_load = false;
2557
+
2558
+ // android (seen in 2.3/Honeycomb) sometimes fails first .load() -> .play(), results in playback failure and ended() events?
2559
+ if (_is_android) {
2560
+ _a._called_load = true;
2561
+ }
2562
+
2563
+ if (_useGlobalHTML5Audio) {
2564
+ _s._global_a = _a;
2565
+ }
2566
+
2567
+ }
2568
+
2569
+ _t.isHTML5 = true;
2570
+
2571
+ // store a ref on the track
2572
+ _t._a = _a;
2573
+
2574
+ // store a ref on the audio
2575
+ _a._t = _t;
2576
+
2577
+ _add_html5_events();
2578
+ _a.loop = (_iO.loops>1?'loop':'');
2579
+
2580
+ if (_iO.autoLoad || _iO.autoPlay) {
2581
+
2582
+ _t.load();
2583
+
2584
+ } else {
2585
+
2586
+ // early HTML5 implementation (non-standard)
2587
+ _a.autobuffer = false;
2588
+
2589
+ // standard
2590
+ _a.preload = 'none';
2591
+
2592
+ }
2593
+
2594
+ // boolean instead of "loop", for webkit? - spec says string. http://www.w3.org/TR/html-markup/audio.html#audio.attrs.loop
2595
+ _a.loop = (_iO.loops > 1 ? 'loop' : '');
2596
+
2597
+ return _a;
2598
+
2599
+ };
2600
+
2601
+ _add_html5_events = function() {
2602
+
2603
+ if (_t._a._added_events) {
2604
+ return false;
2605
+ }
2606
+
2607
+ var f;
2608
+
2609
+ function add(oEvt, oFn, bCapture) {
2610
+ return _t._a ? _t._a.addEventListener(oEvt, oFn, bCapture||false) : null;
2611
+ }
2612
+
2613
+ _s._wD(_h5+'adding event listeners: '+_t.sID);
2614
+ _t._a._added_events = true;
2615
+
2616
+ for (f in _html5_events) {
2617
+ if (_html5_events.hasOwnProperty(f)) {
2618
+ add(f, _html5_events[f]);
2619
+ }
2620
+ }
2621
+
2622
+ return true;
2623
+
2624
+ };
2625
+
2626
+ _remove_html5_events = function() {
2627
+
2628
+ // Remove event listeners
2629
+
2630
+ var f;
2631
+
2632
+ function remove(oEvt, oFn, bCapture) {
2633
+ return (_t._a ? _t._a.removeEventListener(oEvt, oFn, bCapture||false) : null);
2634
+ }
2635
+
2636
+ _s._wD(_h5+'removing event listeners: '+_t.sID);
2637
+ _t._a._added_events = false;
2638
+
2639
+ for (f in _html5_events) {
2640
+ if (_html5_events.hasOwnProperty(f)) {
2641
+ remove(f, _html5_events[f]);
2642
+ }
2643
+ }
2644
+
2645
+ };
2646
+
2647
+ /**
2648
+ * Pseudo-private event internals
2649
+ * ------------------------------
2650
+ */
2651
+
2652
+ this._onload = function(nSuccess) {
2653
+
2654
+
2655
+ var fN, loadOK = !!(nSuccess);
2656
+
2657
+ // <d>
2658
+ fN = 'SMSound._onload(): ';
2659
+ _s._wD(fN + '"' + _t.sID + '"' + (loadOK?' loaded.':' failed to load? - ' + _t.url), (loadOK?1:2));
2660
+ if (!loadOK && !_t.isHTML5) {
2661
+ if (_s.sandbox.noRemote === true) {
2662
+ _s._wD(fN + _str('noNet'), 1);
2663
+ }
2664
+ if (_s.sandbox.noLocal === true) {
2665
+ _s._wD(fN + _str('noLocal'), 1);
2666
+ }
2667
+ }
2668
+ // </d>
2669
+
2670
+ _t.loaded = loadOK;
2671
+ _t.readyState = loadOK?3:2;
2672
+ _t._onbufferchange(0);
2673
+
2674
+ if (_t._iO.onload) {
2675
+ _t._iO.onload.apply(_t, [loadOK]);
2676
+ }
2677
+
2678
+ return true;
2679
+
2680
+ };
2681
+
2682
+ this._onbufferchange = function(nIsBuffering) {
2683
+
2684
+ if (_t.playState === 0) {
2685
+ // ignore if not playing
2686
+ return false;
2687
+ }
2688
+
2689
+ if ((nIsBuffering && _t.isBuffering) || (!nIsBuffering && !_t.isBuffering)) {
2690
+ return false;
2691
+ }
2692
+
2693
+ _t.isBuffering = (nIsBuffering === 1);
2694
+ if (_t._iO.onbufferchange) {
2695
+ _s._wD('SMSound._onbufferchange(): ' + nIsBuffering);
2696
+ _t._iO.onbufferchange.apply(_t);
2697
+ }
2698
+
2699
+ return true;
2700
+
2701
+ };
2702
+
2703
+ /**
2704
+ * Notify Mobile Safari that user action is required
2705
+ * to continue playing / loading the audio file.
2706
+ */
2707
+
2708
+ this._onsuspend = function() {
2709
+
2710
+ if (_t._iO.onsuspend) {
2711
+ _s._wD('SMSound._onsuspend()');
2712
+ _t._iO.onsuspend.apply(_t);
2713
+ }
2714
+
2715
+ return true;
2716
+
2717
+ };
2718
+
2719
+ /**
2720
+ * flash 9/movieStar + RTMP-only method, should fire only once at most
2721
+ * at this point we just recreate failed sounds rather than trying to reconnect
2722
+ */
2723
+
2724
+ this._onfailure = function(msg, level, code) {
2725
+
2726
+ _t.failures++;
2727
+ _s._wD('SMSound._onfailure(): "'+_t.sID+'" count '+_t.failures);
2728
+
2729
+ if (_t._iO.onfailure && _t.failures === 1) {
2730
+ _t._iO.onfailure(_t, msg, level, code);
2731
+ } else {
2732
+ _s._wD('SMSound._onfailure(): ignoring');
2733
+ }
2734
+
2735
+ };
2736
+
2737
+ this._onfinish = function() {
2738
+
2739
+ // store local copy before it gets trashed..
2740
+ var _io_onfinish = _t._iO.onfinish;
2741
+
2742
+ _t._onbufferchange(0);
2743
+ _t._resetOnPosition(0);
2744
+
2745
+ // reset some state items
2746
+ if (_t.instanceCount) {
2747
+
2748
+ _t.instanceCount--;
2749
+
2750
+ if (!_t.instanceCount) {
2751
+
2752
+ // remove onPosition listeners, if any
2753
+ _detachOnPosition();
2754
+
2755
+ // reset instance options
2756
+ _t.playState = 0;
2757
+ _t.paused = false;
2758
+ _t.instanceCount = 0;
2759
+ _t.instanceOptions = {};
2760
+ _t._iO = {};
2761
+ _stop_html5_timer();
2762
+
2763
+ }
2764
+
2765
+ if (!_t.instanceCount || _t._iO.multiShotEvents) {
2766
+ // fire onfinish for last, or every instance
2767
+ if (_io_onfinish) {
2768
+ _s._wD('SMSound._onfinish(): "' + _t.sID + '"');
2769
+ _io_onfinish.apply(_t);
2770
+ }
2771
+ }
2772
+
2773
+ }
2774
+
2775
+ };
2776
+
2777
+ this._whileloading = function(nBytesLoaded, nBytesTotal, nDuration, nBufferLength) {
2778
+
2779
+ var _iO = _t._iO;
2780
+
2781
+ _t.bytesLoaded = nBytesLoaded;
2782
+ _t.bytesTotal = nBytesTotal;
2783
+ _t.duration = Math.floor(nDuration);
2784
+ _t.bufferLength = nBufferLength;
2785
+
2786
+ if (!_iO.isMovieStar) {
2787
+
2788
+ if (_iO.duration) {
2789
+ // use options, if specified and larger
2790
+ _t.durationEstimate = (_t.duration > _iO.duration) ? _t.duration : _iO.duration;
2791
+ } else {
2792
+ _t.durationEstimate = parseInt((_t.bytesTotal / _t.bytesLoaded) * _t.duration, 10);
2793
+
2794
+ }
2795
+
2796
+ if (_t.durationEstimate === undefined) {
2797
+ _t.durationEstimate = _t.duration;
2798
+ }
2799
+
2800
+ if (_t.readyState !== 3 && _iO.whileloading) {
2801
+ _iO.whileloading.apply(_t);
2802
+ }
2803
+
2804
+ } else {
2805
+
2806
+ _t.durationEstimate = _t.duration;
2807
+ if (_t.readyState !== 3 && _iO.whileloading) {
2808
+ _iO.whileloading.apply(_t);
2809
+ }
2810
+
2811
+ }
2812
+
2813
+ };
2814
+
2815
+ this._whileplaying = function(nPosition, oPeakData, oWaveformDataLeft, oWaveformDataRight, oEQData) {
2816
+
2817
+ var _iO = _t._iO,
2818
+ eqLeft;
2819
+
2820
+ if (isNaN(nPosition) || nPosition === null) {
2821
+ // flash safety net
2822
+ return false;
2823
+ }
2824
+
2825
+ _t.position = nPosition;
2826
+ _t._processOnPosition();
2827
+
2828
+ if (!_t.isHTML5 && _fV > 8) {
2829
+
2830
+ if (_iO.usePeakData && typeof oPeakData !== 'undefined' && oPeakData) {
2831
+ _t.peakData = {
2832
+ left: oPeakData.leftPeak,
2833
+ right: oPeakData.rightPeak
2834
+ };
2835
+ }
2836
+
2837
+ if (_iO.useWaveformData && typeof oWaveformDataLeft !== 'undefined' && oWaveformDataLeft) {
2838
+ _t.waveformData = {
2839
+ left: oWaveformDataLeft.split(','),
2840
+ right: oWaveformDataRight.split(',')
2841
+ };
2842
+ }
2843
+
2844
+ if (_iO.useEQData) {
2845
+ if (typeof oEQData !== 'undefined' && oEQData && oEQData.leftEQ) {
2846
+ eqLeft = oEQData.leftEQ.split(',');
2847
+ _t.eqData = eqLeft;
2848
+ _t.eqData.left = eqLeft;
2849
+ if (typeof oEQData.rightEQ !== 'undefined' && oEQData.rightEQ) {
2850
+ _t.eqData.right = oEQData.rightEQ.split(',');
2851
+ }
2852
+ }
2853
+ }
2854
+
2855
+ }
2856
+
2857
+ if (_t.playState === 1) {
2858
+
2859
+ // special case/hack: ensure buffering is false if loading from cache (and not yet started)
2860
+ if (!_t.isHTML5 && _fV === 8 && !_t.position && _t.isBuffering) {
2861
+ _t._onbufferchange(0);
2862
+ }
2863
+
2864
+ if (_iO.whileplaying) {
2865
+ // flash may call after actual finish
2866
+ _iO.whileplaying.apply(_t);
2867
+ }
2868
+
2869
+ }
2870
+
2871
+ return true;
2872
+
2873
+ };
2874
+
2875
+ this._onmetadata = function(oMDProps, oMDData) {
2876
+
2877
+ /**
2878
+ * internal: flash 9 + NetStream (MovieStar/RTMP-only) feature
2879
+ * RTMP may include song title, MovieStar content may include encoding info
2880
+ *
2881
+ * @param {array} oMDProps (names)
2882
+ * @param {array} oMDData (values)
2883
+ */
2884
+
2885
+ _s._wD('SMSound._onmetadata(): "' + this.sID + '" metadata received.');
2886
+
2887
+ var oData = {}, i, j;
2888
+
2889
+ for (i = 0, j = oMDProps.length; i < j; i++) {
2890
+ oData[oMDProps[i]] = oMDData[i];
2891
+ }
2892
+ _t.metadata = oData;
2893
+
2894
+ if (_t._iO.onmetadata) {
2895
+ _t._iO.onmetadata.apply(_t);
2896
+ }
2897
+
2898
+ };
2899
+
2900
+ this._onid3 = function(oID3Props, oID3Data) {
2901
+
2902
+ /**
2903
+ * internal: flash 8 + flash 9 ID3 feature
2904
+ * may include artist, song title etc.
2905
+ *
2906
+ * @param {array} oID3Props (names)
2907
+ * @param {array} oID3Data (values)
2908
+ */
2909
+
2910
+ _s._wD('SMSound._onid3(): "' + this.sID + '" ID3 data received.');
2911
+
2912
+ var oData = [], i, j;
2913
+
2914
+ for (i = 0, j = oID3Props.length; i < j; i++) {
2915
+ oData[oID3Props[i]] = oID3Data[i];
2916
+ }
2917
+ _t.id3 = _mixin(_t.id3, oData);
2918
+
2919
+ if (_t._iO.onid3) {
2920
+ _t._iO.onid3.apply(_t);
2921
+ }
2922
+
2923
+ };
2924
+
2925
+ // flash/RTMP-only
2926
+
2927
+ this._onconnect = function(bSuccess) {
2928
+
2929
+ bSuccess = (bSuccess === 1);
2930
+ _s._wD('SMSound._onconnect(): "'+_t.sID+'"'+(bSuccess?' connected.':' failed to connect? - '+_t.url), (bSuccess?1:2));
2931
+ _t.connected = bSuccess;
2932
+
2933
+ if (bSuccess) {
2934
+
2935
+ _t.failures = 0;
2936
+
2937
+ if (_idCheck(_t.sID)) {
2938
+ if (_t.getAutoPlay()) {
2939
+ // only update the play state if auto playing
2940
+ _t.play(undefined, _t.getAutoPlay());
2941
+ } else if (_t._iO.autoLoad) {
2942
+ _t.load();
2943
+ }
2944
+ }
2945
+
2946
+ if (_t._iO.onconnect) {
2947
+ _t._iO.onconnect.apply(_t, [bSuccess]);
2948
+ }
2949
+
2950
+ }
2951
+
2952
+ };
2953
+
2954
+ this._ondataerror = function(sError) {
2955
+
2956
+ // flash 9 wave/eq data handler
2957
+ // hack: called at start, and end from flash at/after onfinish()
2958
+ if (_t.playState > 0) {
2959
+ _s._wD('SMSound._ondataerror(): ' + sError);
2960
+ if (_t._iO.ondataerror) {
2961
+ _t._iO.ondataerror.apply(_t);
2962
+ }
2963
+ }
2964
+
2965
+ };
2966
+
2967
+ }; // SMSound()
2968
+
2969
+ /**
2970
+ * Private SoundManager internals
2971
+ * ------------------------------
2972
+ */
2973
+
2974
+ _getDocument = function() {
2975
+
2976
+ return (_doc.body || _doc._docElement || _doc.getElementsByTagName('div')[0]);
2977
+
2978
+ };
2979
+
2980
+ _id = function(sID) {
2981
+
2982
+ return _doc.getElementById(sID);
2983
+
2984
+ };
2985
+
2986
+ _mixin = function(oMain, oAdd) {
2987
+
2988
+ // non-destructive merge
2989
+ var o1 = {}, i, o2, o;
2990
+
2991
+ // clone c1
2992
+ for (i in oMain) {
2993
+ if (oMain.hasOwnProperty(i)) {
2994
+ o1[i] = oMain[i];
2995
+ }
2996
+ }
2997
+
2998
+ o2 = (typeof oAdd === 'undefined'?_s.defaultOptions:oAdd);
2999
+ for (o in o2) {
3000
+ if (o2.hasOwnProperty(o) && typeof o1[o] === 'undefined') {
3001
+ o1[o] = o2[o];
3002
+ }
3003
+ }
3004
+ return o1;
3005
+
3006
+ };
3007
+
3008
+ _event = (function() {
3009
+
3010
+ var old = (_win.attachEvent),
3011
+ evt = {
3012
+ add: (old?'attachEvent':'addEventListener'),
3013
+ remove: (old?'detachEvent':'removeEventListener')
3014
+ };
3015
+
3016
+ function getArgs(oArgs) {
3017
+
3018
+ var args = _slice.call(oArgs), len = args.length;
3019
+
3020
+ if (old) {
3021
+ // prefix
3022
+ args[1] = 'on' + args[1];
3023
+ if (len > 3) {
3024
+ // no capture
3025
+ args.pop();
3026
+ }
3027
+ } else if (len === 3) {
3028
+ args.push(false);
3029
+ }
3030
+
3031
+ return args;
3032
+
3033
+ }
3034
+
3035
+ function apply(args, sType) {
3036
+
3037
+ var element = args.shift(),
3038
+ method = [evt[sType]];
3039
+
3040
+ if (old) {
3041
+ element[method](args[0], args[1]);
3042
+ } else {
3043
+ element[method].apply(element, args);
3044
+ }
3045
+
3046
+ }
3047
+
3048
+ function add() {
3049
+
3050
+ apply(getArgs(arguments), 'add');
3051
+
3052
+ }
3053
+
3054
+ function remove() {
3055
+
3056
+ apply(getArgs(arguments), 'remove');
3057
+
3058
+ }
3059
+
3060
+ return {
3061
+ 'add': add,
3062
+ 'remove': remove
3063
+ };
3064
+
3065
+ }());
3066
+
3067
+ /**
3068
+ * Internal HTML5 event handling
3069
+ * -----------------------------
3070
+ */
3071
+
3072
+ function _html5_event(oFn) {
3073
+
3074
+ // wrap html5 event handlers so we don't call them on destroyed sounds
3075
+
3076
+ return function(e) {
3077
+
3078
+ var t = this._t;
3079
+
3080
+ if (!t || !t._a) {
3081
+ // <d>
3082
+ if (t && t.sID) {
3083
+ _s._wD(_h5+'ignoring '+e.type+': '+t.sID);
3084
+ } else {
3085
+ _s._wD(_h5+'ignoring '+e.type);
3086
+ }
3087
+ // </d>
3088
+ return null;
3089
+ } else {
3090
+ return oFn.call(this, e);
3091
+ }
3092
+
3093
+ };
3094
+
3095
+ }
3096
+
3097
+ _html5_events = {
3098
+
3099
+ // HTML5 event-name-to-handler map
3100
+
3101
+ abort: _html5_event(function() {
3102
+
3103
+ _s._wD(_h5+'abort: '+this._t.sID);
3104
+
3105
+ }),
3106
+
3107
+ // enough has loaded to play
3108
+
3109
+ canplay: _html5_event(function() {
3110
+
3111
+ var t = this._t,
3112
+ position1K;
3113
+
3114
+ if (t._html5_canplay) {
3115
+ // this event has already fired. ignore.
3116
+ return true;
3117
+ }
3118
+
3119
+ t._html5_canplay = true;
3120
+ _s._wD(_h5+'canplay: '+t.sID+', '+t.url);
3121
+ t._onbufferchange(0);
3122
+ position1K = (!isNaN(t.position)?t.position/1000:null);
3123
+
3124
+ // set the position if position was set before the sound loaded
3125
+ if (t.position && this.currentTime !== position1K) {
3126
+ _s._wD(_h5+'canplay: setting position to '+position1K);
3127
+ try {
3128
+ this.currentTime = position1K;
3129
+ } catch(ee) {
3130
+ _s._wD(_h5+'setting position failed: '+ee.message, 2);
3131
+ }
3132
+ }
3133
+
3134
+ // hack for HTML5 from/to case
3135
+ if (t._iO._oncanplay) {
3136
+ t._iO._oncanplay();
3137
+ }
3138
+
3139
+ }),
3140
+
3141
+ load: _html5_event(function() {
3142
+
3143
+ var t = this._t;
3144
+
3145
+ if (!t.loaded) {
3146
+ t._onbufferchange(0);
3147
+ // should be 1, and the same
3148
+ t._whileloading(t.bytesTotal, t.bytesTotal, t._get_html5_duration());
3149
+ t._onload(true);
3150
+ }
3151
+
3152
+ }),
3153
+
3154
+ // TODO: Reserved for potential use
3155
+ /*
3156
+ emptied: _html5_event(function() {
3157
+
3158
+ _s._wD(_h5+'emptied: '+this._t.sID);
3159
+
3160
+ }),
3161
+ */
3162
+
3163
+ ended: _html5_event(function() {
3164
+
3165
+ var t = this._t;
3166
+
3167
+ _s._wD(_h5+'ended: '+t.sID);
3168
+ t._onfinish();
3169
+
3170
+ }),
3171
+
3172
+ error: _html5_event(function() {
3173
+
3174
+ _s._wD(_h5+'error: '+this.error.code);
3175
+ // call load with error state?
3176
+ this._t._onload(false);
3177
+
3178
+ }),
3179
+
3180
+ loadeddata: _html5_event(function() {
3181
+
3182
+ var t = this._t,
3183
+ // at least 1 byte, so math works
3184
+ bytesTotal = t.bytesTotal || 1;
3185
+
3186
+ _s._wD(_h5+'loadeddata: '+this._t.sID);
3187
+
3188
+ // safari seems to nicely report progress events, eventually totalling 100%
3189
+ if (!t._loaded && !_isSafari) {
3190
+ t.duration = t._get_html5_duration();
3191
+ // fire whileloading() with 100% values
3192
+ t._whileloading(bytesTotal, bytesTotal, t._get_html5_duration());
3193
+ t._onload(true);
3194
+ }
3195
+
3196
+ }),
3197
+
3198
+ loadedmetadata: _html5_event(function() {
3199
+
3200
+ _s._wD(_h5+'loadedmetadata: '+this._t.sID);
3201
+
3202
+ }),
3203
+
3204
+ loadstart: _html5_event(function() {
3205
+
3206
+ _s._wD(_h5+'loadstart: '+this._t.sID);
3207
+ // assume buffering at first
3208
+ this._t._onbufferchange(1);
3209
+
3210
+ }),
3211
+
3212
+ play: _html5_event(function() {
3213
+
3214
+ _s._wD(_h5+'play: '+this._t.sID+', '+this._t.url);
3215
+ // once play starts, no buffering
3216
+ this._t._onbufferchange(0);
3217
+
3218
+ }),
3219
+
3220
+ playing: _html5_event(function() {
3221
+
3222
+ _s._wD(_h5+'playing: '+this._t.sID);
3223
+
3224
+ // once play starts, no buffering
3225
+ this._t._onbufferchange(0);
3226
+
3227
+ }),
3228
+
3229
+ progress: _html5_event(function(e) {
3230
+
3231
+ var t = this._t,
3232
+ i, j, str, buffered = 0,
3233
+ isProgress = (e.type === 'progress'),
3234
+ ranges = e.target.buffered,
3235
+ // firefox 3.6 implements e.loaded/total (bytes)
3236
+ loaded = (e.loaded||0),
3237
+ total = (e.total||1);
3238
+
3239
+ if (t.loaded) {
3240
+ return false;
3241
+ }
3242
+
3243
+ if (ranges && ranges.length) {
3244
+
3245
+ // if loaded is 0, try TimeRanges implementation as % of load
3246
+ // https://developer.mozilla.org/en/DOM/TimeRanges
3247
+
3248
+ for (i=ranges.length-1; i >= 0; i--) {
3249
+ buffered = (ranges.end(i) - ranges.start(i));
3250
+ }
3251
+
3252
+ // linear case, buffer sum; does not account for seeking and HTTP partials / byte ranges
3253
+ loaded = buffered/e.target.duration;
3254
+
3255
+ // <d>
3256
+ if (isProgress && ranges.length > 1) {
3257
+ str = [];
3258
+ j = ranges.length;
3259
+ for (i=0; i<j; i++) {
3260
+ str.push(e.target.buffered.start(i) +'-'+ e.target.buffered.end(i));
3261
+ }
3262
+ _s._wD(_h5+'progress: timeRanges: '+str.join(', '));
3263
+ }
3264
+
3265
+ if (isProgress && !isNaN(loaded)) {
3266
+ _s._wD(_h5+'progress: '+t.sID+': ' + Math.floor(loaded*100)+'% loaded');
3267
+ }
3268
+ // </d>
3269
+
3270
+ }
3271
+
3272
+ if (!isNaN(loaded)) {
3273
+
3274
+ // if progress, likely not buffering
3275
+ t._onbufferchange(0);
3276
+ t._whileloading(loaded, total, t._get_html5_duration());
3277
+ if (loaded && total && loaded === total) {
3278
+ // in case "onload" doesn't fire (eg. gecko 1.9.2)
3279
+ _html5_events.load.call(this, e);
3280
+ }
3281
+
3282
+ }
3283
+
3284
+ }),
3285
+
3286
+ ratechange: _html5_event(function() {
3287
+
3288
+ _s._wD(_h5+'ratechange: '+this._t.sID);
3289
+
3290
+ }),
3291
+
3292
+ suspend: _html5_event(function(e) {
3293
+
3294
+ // download paused/stopped, may have finished (eg. onload)
3295
+ var t = this._t;
3296
+
3297
+ _s._wD(_h5+'suspend: '+t.sID);
3298
+ _html5_events.progress.call(this, e);
3299
+ t._onsuspend();
3300
+
3301
+ }),
3302
+
3303
+ stalled: _html5_event(function() {
3304
+
3305
+ _s._wD(_h5+'stalled: '+this._t.sID);
3306
+
3307
+ }),
3308
+
3309
+ timeupdate: _html5_event(function() {
3310
+
3311
+ this._t._onTimer();
3312
+
3313
+ }),
3314
+
3315
+ waiting: _html5_event(function() {
3316
+
3317
+ var t = this._t;
3318
+
3319
+ // see also: seeking
3320
+ _s._wD(_h5+'waiting: '+t.sID);
3321
+
3322
+ // playback faster than download rate, etc.
3323
+ t._onbufferchange(1);
3324
+
3325
+ })
3326
+
3327
+ };
3328
+
3329
+ _html5OK = function(iO) {
3330
+
3331
+ // Use type, if specified. If HTML5-only mode, no other options, so just give 'er
3332
+ return (!iO.serverURL && (iO.type?_html5CanPlay({type:iO.type}):_html5CanPlay({url:iO.url})||_s.html5Only));
3333
+
3334
+ };
3335
+
3336
+ _html5Unload = function(oAudio) {
3337
+
3338
+ /**
3339
+ * Internal method: Unload media, and cancel any current/pending network requests.
3340
+ * Firefox can load an empty URL, which allegedly destroys the decoder and stops the download.
3341
+ * https://developer.mozilla.org/En/Using_audio_and_video_in_Firefox#Stopping_the_download_of_media
3342
+ * Other UA behaviour is unclear, so everyone else gets an about:blank-style URL.
3343
+ */
3344
+
3345
+ if (oAudio) {
3346
+ // Firefox likes '' for unload, most other UAs don't and fail to unload.
3347
+ oAudio.src = (_is_firefox ? '' : _emptyURL);
3348
+ }
3349
+
3350
+ };
3351
+
3352
+ _html5CanPlay = function(o) {
3353
+
3354
+ /**
3355
+ * Try to find MIME, test and return truthiness
3356
+ * o = {
3357
+ * url: '/path/to/an.mp3',
3358
+ * type: 'audio/mp3'
3359
+ * }
3360
+ */
3361
+
3362
+ if (!_s.useHTML5Audio || !_s.hasHTML5) {
3363
+ return false;
3364
+ }
3365
+
3366
+ var url = (o.url || null),
3367
+ mime = (o.type || null),
3368
+ aF = _s.audioFormats,
3369
+ result,
3370
+ offset,
3371
+ fileExt,
3372
+ item;
3373
+
3374
+ function preferFlashCheck(kind) {
3375
+
3376
+ // whether flash should play a given type
3377
+ return (_s.preferFlash && _hasFlash && !_s.ignoreFlash && (typeof _s.flash[kind] !== 'undefined' && _s.flash[kind]));
3378
+
3379
+ }
3380
+
3381
+ // account for known cases like audio/mp3
3382
+
3383
+ if (mime && typeof _s.html5[mime] !== 'undefined') {
3384
+ return (_s.html5[mime] && !preferFlashCheck(mime));
3385
+ }
3386
+
3387
+ if (!_html5Ext) {
3388
+ _html5Ext = [];
3389
+ for (item in aF) {
3390
+ if (aF.hasOwnProperty(item)) {
3391
+ _html5Ext.push(item);
3392
+ if (aF[item].related) {
3393
+ _html5Ext = _html5Ext.concat(aF[item].related);
3394
+ }
3395
+ }
3396
+ }
3397
+ _html5Ext = new RegExp('\\.('+_html5Ext.join('|')+')(\\?.*)?$','i');
3398
+ }
3399
+
3400
+ // TODO: Strip URL queries, etc.
3401
+ fileExt = (url ? url.toLowerCase().match(_html5Ext) : null);
3402
+
3403
+ if (!fileExt || !fileExt.length) {
3404
+ if (!mime) {
3405
+ return false;
3406
+ } else {
3407
+ // audio/mp3 -> mp3, result should be known
3408
+ offset = mime.indexOf(';');
3409
+ // strip "audio/X; codecs.."
3410
+ fileExt = (offset !== -1?mime.substr(0,offset):mime).substr(6);
3411
+ }
3412
+ } else {
3413
+ // match the raw extension name - "mp3", for example
3414
+ fileExt = fileExt[1];
3415
+ }
3416
+
3417
+ if (fileExt && typeof _s.html5[fileExt] !== 'undefined') {
3418
+ // result known
3419
+ return (_s.html5[fileExt] && !preferFlashCheck(fileExt));
3420
+ } else {
3421
+ mime = 'audio/'+fileExt;
3422
+ result = _s.html5.canPlayType({type:mime});
3423
+ _s.html5[fileExt] = result;
3424
+ // _s._wD('canPlayType, found result: '+result);
3425
+ return (result && _s.html5[mime] && !preferFlashCheck(mime));
3426
+ }
3427
+
3428
+ };
3429
+
3430
+ _testHTML5 = function() {
3431
+
3432
+ if (!_s.useHTML5Audio || typeof Audio === 'undefined') {
3433
+ return false;
3434
+ }
3435
+
3436
+ // double-whammy: Opera 9.64 throws WRONG_ARGUMENTS_ERR if no parameter passed to Audio(), and Webkit + iOS happily tries to load "null" as a URL. :/
3437
+ var a = (typeof Audio !== 'undefined' ? (_isOpera ? new Audio(null) : new Audio()) : null),
3438
+ item, support = {}, aF, i;
3439
+
3440
+ function _cp(m) {
3441
+
3442
+ var canPlay, i, j, isOK = false;
3443
+
3444
+ if (!a || typeof a.canPlayType !== 'function') {
3445
+ return false;
3446
+ }
3447
+
3448
+ if (m instanceof Array) {
3449
+ // iterate through all mime types, return any successes
3450
+ for (i=0, j=m.length; i<j && !isOK; i++) {
3451
+ if (_s.html5[m[i]] || a.canPlayType(m[i]).match(_s.html5Test)) {
3452
+ isOK = true;
3453
+ _s.html5[m[i]] = true;
3454
+
3455
+ // if flash can play and preferred, also mark it for use.
3456
+ _s.flash[m[i]] = !!(_s.preferFlash && _hasFlash && m[i].match(_flashMIME));
3457
+
3458
+ }
3459
+ }
3460
+ return isOK;
3461
+ } else {
3462
+ canPlay = (a && typeof a.canPlayType === 'function' ? a.canPlayType(m) : false);
3463
+ return !!(canPlay && (canPlay.match(_s.html5Test)));
3464
+ }
3465
+
3466
+ }
3467
+
3468
+ // test all registered formats + codecs
3469
+
3470
+ aF = _s.audioFormats;
3471
+
3472
+ for (item in aF) {
3473
+ if (aF.hasOwnProperty(item)) {
3474
+ support[item] = _cp(aF[item].type);
3475
+
3476
+ // write back generic type too, eg. audio/mp3
3477
+ support['audio/'+item] = support[item];
3478
+
3479
+ // assign flash
3480
+ if (_s.preferFlash && !_s.ignoreFlash && item.match(_flashMIME)) {
3481
+ _s.flash[item] = true;
3482
+ } else {
3483
+ _s.flash[item] = false;
3484
+ }
3485
+
3486
+ // assign result to related formats, too
3487
+ if (aF[item] && aF[item].related) {
3488
+ for (i=aF[item].related.length-1; i >= 0; i--) {
3489
+ // eg. audio/m4a
3490
+ support['audio/'+aF[item].related[i]] = support[item];
3491
+ _s.html5[aF[item].related[i]] = support[item];
3492
+ _s.flash[aF[item].related[i]] = support[item];
3493
+ }
3494
+ }
3495
+ }
3496
+ }
3497
+
3498
+ support.canPlayType = (a?_cp:null);
3499
+ _s.html5 = _mixin(_s.html5, support);
3500
+
3501
+ return true;
3502
+
3503
+ };
3504
+
3505
+ _strings = {
3506
+
3507
+ // <d>
3508
+ notReady: 'Not loaded yet - wait for soundManager.onload()/onready()',
3509
+ notOK: 'Audio support is not available.',
3510
+ domError: _smc + 'createMovie(): appendChild/innerHTML call failed. DOM not ready or other error.',
3511
+ spcWmode: _smc + 'createMovie(): Removing wmode, preventing known SWF loading issue(s)',
3512
+ swf404: _sm + ': Verify that %s is a valid path.',
3513
+ tryDebug: 'Try ' + _sm + '.debugFlash = true for more security details (output goes to SWF.)',
3514
+ checkSWF: 'See SWF output for more debug info.',
3515
+ localFail: _sm + ': Non-HTTP page (' + _doc.location.protocol + ' URL?) Review Flash player security settings for this special case:\nhttp://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html\nMay need to add/allow path, eg. c:/sm2/ or /users/me/sm2/',
3516
+ waitFocus: _sm + ': Special case: Waiting for focus-related event..',
3517
+ waitImpatient: _sm + ': Getting impatient, still waiting for Flash%s...',
3518
+ waitForever: _sm + ': Waiting indefinitely for Flash (will recover if unblocked)...',
3519
+ needFunction: _sm + ': Function object expected for %s',
3520
+ badID: 'Warning: Sound ID "%s" should be a string, starting with a non-numeric character',
3521
+ currentObj: '--- ' + _sm + '._debug(): Current sound objects ---',
3522
+ waitEI: _smc + 'initMovie(): Waiting for ExternalInterface call from Flash..',
3523
+ waitOnload: _sm + ': Waiting for window.onload()',
3524
+ docLoaded: _sm + ': Document already loaded',
3525
+ onload: _smc + 'initComplete(): calling soundManager.onload()',
3526
+ onloadOK: _sm + '.onload() complete',
3527
+ init: _smc + 'init()',
3528
+ didInit: _smc + 'init(): Already called?',
3529
+ flashJS: _sm + ': Attempting to call Flash from JS..',
3530
+ secNote: 'Flash security note: Network/internet URLs will not load due to security restrictions. Access can be configured via Flash Player Global Security Settings Page: http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04.html',
3531
+ badRemove: 'Warning: Failed to remove flash movie.',
3532
+ noPeak: 'Warning: peakData features unsupported for movieStar formats',
3533
+ shutdown: _sm + '.disable(): Shutting down',
3534
+ queue: _sm + ': Queueing %s handler',
3535
+ smFail: _sm + ': Failed to initialise.',
3536
+ smError: 'SMSound.load(): Exception: JS-Flash communication failed, or JS error.',
3537
+ fbTimeout: 'No flash response, applying .'+_swfCSS.swfTimedout+' CSS..',
3538
+ fbLoaded: 'Flash loaded',
3539
+ fbHandler: _smc+'flashBlockHandler()',
3540
+ manURL: 'SMSound.load(): Using manually-assigned URL',
3541
+ onURL: _sm + '.load(): current URL already assigned.',
3542
+ badFV: _sm + '.flashVersion must be 8 or 9. "%s" is invalid. Reverting to %s.',
3543
+ as2loop: 'Note: Setting stream:false so looping can work (flash 8 limitation)',
3544
+ noNSLoop: 'Note: Looping not implemented for MovieStar formats',
3545
+ needfl9: 'Note: Switching to flash 9, required for MP4 formats.',
3546
+ mfTimeout: 'Setting flashLoadTimeout = 0 (infinite) for off-screen, mobile flash case',
3547
+ mfOn: 'mobileFlash::enabling on-screen flash repositioning',
3548
+ policy: 'Enabling usePolicyFile for data access'
3549
+ // </d>
3550
+
3551
+ };
3552
+
3553
+ _str = function() {
3554
+
3555
+ // internal string replace helper.
3556
+ // arguments: o [,items to replace]
3557
+ // <d>
3558
+
3559
+ // real array, please
3560
+ var args = _slice.call(arguments),
3561
+
3562
+ // first arg
3563
+ o = args.shift(),
3564
+
3565
+ str = (_strings && _strings[o]?_strings[o]:''), i, j;
3566
+ if (str && args && args.length) {
3567
+ for (i = 0, j = args.length; i < j; i++) {
3568
+ str = str.replace('%s', args[i]);
3569
+ }
3570
+ }
3571
+
3572
+ return str;
3573
+ // </d>
3574
+
3575
+ };
3576
+
3577
+ _loopFix = function(sOpt) {
3578
+
3579
+ // flash 8 requires stream = false for looping to work
3580
+ if (_fV === 8 && sOpt.loops > 1 && sOpt.stream) {
3581
+ _wDS('as2loop');
3582
+ sOpt.stream = false;
3583
+ }
3584
+
3585
+ return sOpt;
3586
+
3587
+ };
3588
+
3589
+ _policyFix = function(sOpt, sPre) {
3590
+
3591
+ if (sOpt && !sOpt.usePolicyFile && (sOpt.onid3 || sOpt.usePeakData || sOpt.useWaveformData || sOpt.useEQData)) {
3592
+ _s._wD((sPre || '') + _str('policy'));
3593
+ sOpt.usePolicyFile = true;
3594
+ }
3595
+
3596
+ return sOpt;
3597
+
3598
+ };
3599
+
3600
+ _complain = function(sMsg) {
3601
+
3602
+ // <d>
3603
+ if (typeof console !== 'undefined' && typeof console.warn !== 'undefined') {
3604
+ console.warn(sMsg);
3605
+ } else {
3606
+ _s._wD(sMsg);
3607
+ }
3608
+ // </d>
3609
+
3610
+ };
3611
+
3612
+ _doNothing = function() {
3613
+
3614
+ return false;
3615
+
3616
+ };
3617
+
3618
+ _disableObject = function(o) {
3619
+
3620
+ var oProp;
3621
+
3622
+ for (oProp in o) {
3623
+ if (o.hasOwnProperty(oProp) && typeof o[oProp] === 'function') {
3624
+ o[oProp] = _doNothing;
3625
+ }
3626
+ }
3627
+
3628
+ oProp = null;
3629
+
3630
+ };
3631
+
3632
+ _failSafely = function(bNoDisable) {
3633
+
3634
+ // general failure exception handler
3635
+
3636
+ if (typeof bNoDisable === 'undefined') {
3637
+ bNoDisable = false;
3638
+ }
3639
+
3640
+ if (_disabled || bNoDisable) {
3641
+ _wDS('smFail', 2);
3642
+ _s.disable(bNoDisable);
3643
+ }
3644
+
3645
+ };
3646
+
3647
+ _normalizeMovieURL = function(smURL) {
3648
+
3649
+ var urlParams = null, url;
3650
+
3651
+ if (smURL) {
3652
+ if (smURL.match(/\.swf(\?.*)?$/i)) {
3653
+ urlParams = smURL.substr(smURL.toLowerCase().lastIndexOf('.swf?') + 4);
3654
+ if (urlParams) {
3655
+ // assume user knows what they're doing
3656
+ return smURL;
3657
+ }
3658
+ } else if (smURL.lastIndexOf('/') !== smURL.length - 1) {
3659
+ // append trailing slash, if needed
3660
+ smURL += '/';
3661
+ }
3662
+ }
3663
+
3664
+ url = (smURL && smURL.lastIndexOf('/') !== - 1 ? smURL.substr(0, smURL.lastIndexOf('/') + 1) : './') + _s.movieURL;
3665
+
3666
+ if (_s.noSWFCache) {
3667
+ url += ('?ts=' + new Date().getTime());
3668
+ }
3669
+
3670
+ return url;
3671
+
3672
+ };
3673
+
3674
+ _setVersionInfo = function() {
3675
+
3676
+ // short-hand for internal use
3677
+
3678
+ _fV = parseInt(_s.flashVersion, 10);
3679
+
3680
+ if (_fV !== 8 && _fV !== 9) {
3681
+ _s._wD(_str('badFV', _fV, _defaultFlashVersion));
3682
+ _s.flashVersion = _fV = _defaultFlashVersion;
3683
+ }
3684
+
3685
+ // debug flash movie, if applicable
3686
+
3687
+ var isDebug = (_s.debugMode || _s.debugFlash?'_debug.swf':'.swf');
3688
+
3689
+ if (_s.useHTML5Audio && !_s.html5Only && _s.audioFormats.mp4.required && _fV < 9) {
3690
+ _s._wD(_str('needfl9'));
3691
+ _s.flashVersion = _fV = 9;
3692
+ }
3693
+
3694
+ _s.version = _s.versionNumber + (_s.html5Only?' (HTML5-only mode)':(_fV === 9?' (AS3/Flash 9)':' (AS2/Flash 8)'));
3695
+
3696
+ // set up default options
3697
+ if (_fV > 8) {
3698
+ // +flash 9 base options
3699
+ _s.defaultOptions = _mixin(_s.defaultOptions, _s.flash9Options);
3700
+ _s.features.buffering = true;
3701
+ // +moviestar support
3702
+ _s.defaultOptions = _mixin(_s.defaultOptions, _s.movieStarOptions);
3703
+ _s.filePatterns.flash9 = new RegExp('\\.(mp3|' + _netStreamTypes.join('|') + ')(\\?.*)?$', 'i');
3704
+ _s.features.movieStar = true;
3705
+ } else {
3706
+ _s.features.movieStar = false;
3707
+ }
3708
+
3709
+ // regExp for flash canPlay(), etc.
3710
+ _s.filePattern = _s.filePatterns[(_fV !== 8?'flash9':'flash8')];
3711
+
3712
+ // if applicable, use _debug versions of SWFs
3713
+ _s.movieURL = (_fV === 8?'soundmanager2.swf':'soundmanager2_flash9.swf').replace('.swf', isDebug);
3714
+
3715
+ _s.features.peakData = _s.features.waveformData = _s.features.eqData = (_fV > 8);
3716
+
3717
+ };
3718
+
3719
+ _setPolling = function(bPolling, bHighPerformance) {
3720
+
3721
+ if (!_flash) {
3722
+ return false;
3723
+ }
3724
+
3725
+ _flash._setPolling(bPolling, bHighPerformance);
3726
+
3727
+ };
3728
+
3729
+ _initDebug = function() {
3730
+
3731
+ // starts debug mode, creating output <div> for UAs without console object
3732
+
3733
+ // allow force of debug mode via URL
3734
+ if (_s.debugURLParam.test(_wl)) {
3735
+ _s.debugMode = true;
3736
+ }
3737
+
3738
+ // <d>
3739
+ if (_id(_s.debugID)) {
3740
+ return false;
3741
+ }
3742
+
3743
+ var oD, oDebug, oTarget, oToggle, tmp;
3744
+
3745
+ if (_s.debugMode && !_id(_s.debugID) && (!_hasConsole || !_s.useConsole || !_s.consoleOnly)) {
3746
+
3747
+ oD = _doc.createElement('div');
3748
+ oD.id = _s.debugID + '-toggle';
3749
+
3750
+ oToggle = {
3751
+ 'position': 'fixed',
3752
+ 'bottom': '0px',
3753
+ 'right': '0px',
3754
+ 'width': '1.2em',
3755
+ 'height': '1.2em',
3756
+ 'lineHeight': '1.2em',
3757
+ 'margin': '2px',
3758
+ 'textAlign': 'center',
3759
+ 'border': '1px solid #999',
3760
+ 'cursor': 'pointer',
3761
+ 'background': '#fff',
3762
+ 'color': '#333',
3763
+ 'zIndex': 10001
3764
+ };
3765
+
3766
+ oD.appendChild(_doc.createTextNode('-'));
3767
+ oD.onclick = _toggleDebug;
3768
+ oD.title = 'Toggle SM2 debug console';
3769
+
3770
+ if (_ua.match(/msie 6/i)) {
3771
+ oD.style.position = 'absolute';
3772
+ oD.style.cursor = 'hand';
3773
+ }
3774
+
3775
+ for (tmp in oToggle) {
3776
+ if (oToggle.hasOwnProperty(tmp)) {
3777
+ oD.style[tmp] = oToggle[tmp];
3778
+ }
3779
+ }
3780
+
3781
+ oDebug = _doc.createElement('div');
3782
+ oDebug.id = _s.debugID;
3783
+ oDebug.style.display = (_s.debugMode?'block':'none');
3784
+
3785
+ if (_s.debugMode && !_id(oD.id)) {
3786
+ try {
3787
+ oTarget = _getDocument();
3788
+ oTarget.appendChild(oD);
3789
+ } catch(e2) {
3790
+ throw new Error(_str('domError')+' \n'+e2.toString());
3791
+ }
3792
+ oTarget.appendChild(oDebug);
3793
+ }
3794
+
3795
+ }
3796
+
3797
+ oTarget = null;
3798
+ // </d>
3799
+
3800
+ };
3801
+
3802
+ _idCheck = this.getSoundById;
3803
+
3804
+ // <d>
3805
+ _wDS = function(o, errorLevel) {
3806
+
3807
+ if (!o) {
3808
+ return '';
3809
+ } else {
3810
+ return _s._wD(_str(o), errorLevel);
3811
+ }
3812
+
3813
+ };
3814
+
3815
+ // last-resort debugging option
3816
+
3817
+ if (_wl.indexOf('sm2-debug=alert') + 1 && _s.debugMode) {
3818
+ _s._wD = function(sText) {window.alert(sText);};
3819
+ }
3820
+
3821
+ _toggleDebug = function() {
3822
+
3823
+ var o = _id(_s.debugID),
3824
+ oT = _id(_s.debugID + '-toggle');
3825
+
3826
+ if (!o) {
3827
+ return false;
3828
+ }
3829
+
3830
+ if (_debugOpen) {
3831
+ // minimize
3832
+ oT.innerHTML = '+';
3833
+ o.style.display = 'none';
3834
+ } else {
3835
+ oT.innerHTML = '-';
3836
+ o.style.display = 'block';
3837
+ }
3838
+
3839
+ _debugOpen = !_debugOpen;
3840
+
3841
+ };
3842
+
3843
+ _debugTS = function(sEventType, bSuccess, sMessage) {
3844
+
3845
+ // troubleshooter debug hooks
3846
+
3847
+ if (typeof sm2Debugger !== 'undefined') {
3848
+ try {
3849
+ sm2Debugger.handleEvent(sEventType, bSuccess, sMessage);
3850
+ } catch(e) {
3851
+ // oh well
3852
+ }
3853
+ }
3854
+
3855
+ return true;
3856
+
3857
+ };
3858
+ // </d>
3859
+
3860
+ _getSWFCSS = function() {
3861
+
3862
+ var css = [];
3863
+
3864
+ if (_s.debugMode) {
3865
+ css.push(_swfCSS.sm2Debug);
3866
+ }
3867
+
3868
+ if (_s.debugFlash) {
3869
+ css.push(_swfCSS.flashDebug);
3870
+ }
3871
+
3872
+ if (_s.useHighPerformance) {
3873
+ css.push(_swfCSS.highPerf);
3874
+ }
3875
+
3876
+ return css.join(' ');
3877
+
3878
+ };
3879
+
3880
+ _flashBlockHandler = function() {
3881
+
3882
+ // *possible* flash block situation.
3883
+
3884
+ var name = _str('fbHandler'),
3885
+ p = _s.getMoviePercent(),
3886
+ css = _swfCSS,
3887
+ error = {type:'FLASHBLOCK'};
3888
+
3889
+ if (_s.html5Only) {
3890
+ return false;
3891
+ }
3892
+
3893
+ if (!_s.ok()) {
3894
+
3895
+ if (_needsFlash) {
3896
+ // make the movie more visible, so user can fix
3897
+ _s.oMC.className = _getSWFCSS() + ' ' + css.swfDefault + ' ' + (p === null?css.swfTimedout:css.swfError);
3898
+ _s._wD(name+': '+_str('fbTimeout')+(p?' ('+_str('fbLoaded')+')':''));
3899
+ }
3900
+
3901
+ _s.didFlashBlock = true;
3902
+
3903
+ // fire onready(), complain lightly
3904
+ _processOnEvents({type:'ontimeout', ignoreInit:true, error:error});
3905
+ _catchError(error);
3906
+
3907
+ } else {
3908
+
3909
+ // SM2 loaded OK (or recovered)
3910
+
3911
+ // <d>
3912
+ if (_s.didFlashBlock) {
3913
+ _s._wD(name+': Unblocked');
3914
+ }
3915
+ // </d>
3916
+
3917
+ if (_s.oMC) {
3918
+ _s.oMC.className = [_getSWFCSS(), css.swfDefault, css.swfLoaded + (_s.didFlashBlock?' '+css.swfUnblocked:'')].join(' ');
3919
+ }
3920
+
3921
+ }
3922
+
3923
+ };
3924
+
3925
+ _addOnEvent = function(sType, oMethod, oScope) {
3926
+
3927
+ if (typeof _on_queue[sType] === 'undefined') {
3928
+ _on_queue[sType] = [];
3929
+ }
3930
+
3931
+ _on_queue[sType].push({
3932
+ 'method': oMethod,
3933
+ 'scope': (oScope || null),
3934
+ 'fired': false
3935
+ });
3936
+
3937
+ };
3938
+
3939
+ _processOnEvents = function(oOptions) {
3940
+
3941
+ // assume onready, if unspecified
3942
+
3943
+ if (!oOptions) {
3944
+ oOptions = {
3945
+ type: 'onready'
3946
+ };
3947
+ }
3948
+
3949
+ if (!_didInit && oOptions && !oOptions.ignoreInit) {
3950
+ // not ready yet.
3951
+ return false;
3952
+ }
3953
+
3954
+ if (oOptions.type === 'ontimeout' && _s.ok()) {
3955
+ // invalid case
3956
+ return false;
3957
+ }
3958
+
3959
+ var status = {
3960
+ success: (oOptions && oOptions.ignoreInit?_s.ok():!_disabled)
3961
+ },
3962
+
3963
+ // queue specified by type, or none
3964
+ srcQueue = (oOptions && oOptions.type?_on_queue[oOptions.type]||[]:[]),
3965
+
3966
+ queue = [], i, j,
3967
+ args = [status],
3968
+ canRetry = (_needsFlash && _s.useFlashBlock && !_s.ok());
3969
+
3970
+ if (oOptions.error) {
3971
+ args[0].error = oOptions.error;
3972
+ }
3973
+
3974
+ for (i = 0, j = srcQueue.length; i < j; i++) {
3975
+ if (srcQueue[i].fired !== true) {
3976
+ queue.push(srcQueue[i]);
3977
+ }
3978
+ }
3979
+
3980
+ if (queue.length) {
3981
+ _s._wD(_sm + ': Firing ' + queue.length + ' '+oOptions.type+'() item' + (queue.length === 1?'':'s'));
3982
+ for (i = 0, j = queue.length; i < j; i++) {
3983
+ if (queue[i].scope) {
3984
+ queue[i].method.apply(queue[i].scope, args);
3985
+ } else {
3986
+ queue[i].method.apply(this, args);
3987
+ }
3988
+ if (!canRetry) {
3989
+ // flashblock case doesn't count here
3990
+ queue[i].fired = true;
3991
+ }
3992
+ }
3993
+ }
3994
+
3995
+ return true;
3996
+
3997
+ };
3998
+
3999
+ _initUserOnload = function() {
4000
+
4001
+ _win.setTimeout(function() {
4002
+
4003
+ if (_s.useFlashBlock) {
4004
+ _flashBlockHandler();
4005
+ }
4006
+
4007
+ _processOnEvents();
4008
+
4009
+ // call user-defined "onload", scoped to window
4010
+
4011
+ if (_s.onload instanceof Function) {
4012
+ _wDS('onload', 1);
4013
+ _s.onload.apply(_win);
4014
+ _wDS('onloadOK', 1);
4015
+ }
4016
+
4017
+ if (_s.waitForWindowLoad) {
4018
+ _event.add(_win, 'load', _initUserOnload);
4019
+ }
4020
+
4021
+ },1);
4022
+
4023
+ };
4024
+
4025
+ _detectFlash = function() {
4026
+
4027
+ // hat tip: Flash Detect library (BSD, (C) 2007) by Carl "DocYes" S. Yestrau - http://featureblend.com/javascript-flash-detection-library.html / http://featureblend.com/license.txt
4028
+
4029
+ if (_hasFlash !== undefined) {
4030
+ // this work has already been done.
4031
+ return _hasFlash;
4032
+ }
4033
+
4034
+ var hasPlugin = false, n = navigator, nP = n.plugins, obj, type, types, AX = _win.ActiveXObject;
4035
+
4036
+ if (nP && nP.length) {
4037
+ type = 'application/x-shockwave-flash';
4038
+ types = n.mimeTypes;
4039
+ if (types && types[type] && types[type].enabledPlugin && types[type].enabledPlugin.description) {
4040
+ hasPlugin = true;
4041
+ }
4042
+ } else if (typeof AX !== 'undefined') {
4043
+ try {
4044
+ obj = new AX('ShockwaveFlash.ShockwaveFlash');
4045
+ } catch(e) {
4046
+ // oh well
4047
+ }
4048
+ hasPlugin = (!!obj);
4049
+ }
4050
+
4051
+ _hasFlash = hasPlugin;
4052
+
4053
+ return hasPlugin;
4054
+
4055
+ };
4056
+
4057
+ _featureCheck = function() {
4058
+
4059
+ var needsFlash, item,
4060
+
4061
+ // iPhone <= 3.1 has broken HTML5 audio(), but firmware 3.2 (iPad) + iOS4 works.
4062
+ isSpecial = (_is_iDevice && !!(_ua.match(/os (1|2|3_0|3_1)/i)));
4063
+
4064
+ if (isSpecial) {
4065
+
4066
+ // has Audio(), but is broken; let it load links directly.
4067
+ _s.hasHTML5 = false;
4068
+
4069
+ // ignore flash case, however
4070
+ _s.html5Only = true;
4071
+
4072
+ if (_s.oMC) {
4073
+ _s.oMC.style.display = 'none';
4074
+ }
4075
+
4076
+ return false;
4077
+
4078
+ }
4079
+
4080
+ if (_s.useHTML5Audio) {
4081
+
4082
+ if (!_s.html5 || !_s.html5.canPlayType) {
4083
+ _s._wD('SoundManager: No HTML5 Audio() support detected.');
4084
+ _s.hasHTML5 = false;
4085
+ return true;
4086
+ } else {
4087
+ _s.hasHTML5 = true;
4088
+ }
4089
+ if (_isBadSafari) {
4090
+ _s._wD(_smc+'Note: Buggy HTML5 Audio in Safari on this OS X release, see https://bugs.webkit.org/show_bug.cgi?id=32159 - '+(!_hasFlash?' would use flash fallback for MP3/MP4, but none detected.':'will use flash fallback for MP3/MP4, if available'),1);
4091
+ if (_detectFlash()) {
4092
+ return true;
4093
+ }
4094
+ }
4095
+ } else {
4096
+
4097
+ // flash needed (or, HTML5 needs enabling.)
4098
+ return true;
4099
+
4100
+ }
4101
+
4102
+ for (item in _s.audioFormats) {
4103
+ if (_s.audioFormats.hasOwnProperty(item)) {
4104
+ if ((_s.audioFormats[item].required && !_s.html5.canPlayType(_s.audioFormats[item].type)) || _s.flash[item] || _s.flash[_s.audioFormats[item].type]) {
4105
+ // flash may be required, or preferred for this format
4106
+ needsFlash = true;
4107
+ }
4108
+ }
4109
+ }
4110
+
4111
+ // sanity check..
4112
+ if (_s.ignoreFlash) {
4113
+ needsFlash = false;
4114
+ }
4115
+
4116
+ _s.html5Only = (_s.hasHTML5 && _s.useHTML5Audio && !needsFlash);
4117
+
4118
+ return (!_s.html5Only);
4119
+
4120
+ };
4121
+
4122
+ _parseURL = function(url) {
4123
+
4124
+ /**
4125
+ * Internal: Finds and returns the first playable URL (or failing that, the first URL.)
4126
+ * @param {string or array} url A single URL string, OR, an array of URL strings or {url:'/path/to/resource', type:'audio/mp3'} objects.
4127
+ */
4128
+
4129
+ var i, j, result = 0;
4130
+
4131
+ if (url instanceof Array) {
4132
+
4133
+ // find the first good one
4134
+ for (i=0, j=url.length; i<j; i++) {
4135
+
4136
+ if (url[i] instanceof Object) {
4137
+ // MIME check
4138
+ if (_s.canPlayMIME(url[i].type)) {
4139
+ result = i;
4140
+ break;
4141
+ }
4142
+
4143
+ } else if (_s.canPlayURL(url[i])) {
4144
+ // URL string check
4145
+ result = i;
4146
+ break;
4147
+ }
4148
+
4149
+ }
4150
+
4151
+ // normalize to string
4152
+ if (url[result].url) {
4153
+ url[result] = url[result].url;
4154
+ }
4155
+
4156
+ return url[result];
4157
+
4158
+ } else {
4159
+
4160
+ // single URL case
4161
+ return url;
4162
+
4163
+ }
4164
+
4165
+ };
4166
+
4167
+
4168
+ _startTimer = function(oSound) {
4169
+
4170
+ /**
4171
+ * attach a timer to this sound, and start an interval if needed
4172
+ */
4173
+
4174
+ if (!oSound._hasTimer) {
4175
+
4176
+ oSound._hasTimer = true;
4177
+
4178
+ if (!_likesHTML5 && _s.html5PollingInterval) {
4179
+
4180
+ if (_h5IntervalTimer === null && _h5TimerCount === 0) {
4181
+
4182
+ _h5IntervalTimer = window.setInterval(_timerExecute, _s.html5PollingInterval);
4183
+
4184
+ }
4185
+
4186
+ _h5TimerCount++;
4187
+
4188
+ }
4189
+
4190
+ }
4191
+
4192
+ };
4193
+
4194
+ _stopTimer = function(oSound) {
4195
+
4196
+ /**
4197
+ * detach a timer
4198
+ */
4199
+
4200
+ if (oSound._hasTimer) {
4201
+
4202
+ oSound._hasTimer = false;
4203
+
4204
+ if (!_likesHTML5 && _s.html5PollingInterval) {
4205
+
4206
+ // interval will stop itself at next execution.
4207
+
4208
+ _h5TimerCount--;
4209
+
4210
+ }
4211
+
4212
+ }
4213
+
4214
+ };
4215
+
4216
+ _timerExecute = function() {
4217
+
4218
+ /**
4219
+ * manual polling for HTML5 progress events, ie., whileplaying() (can achieve greater precision than conservative default HTML5 interval)
4220
+ */
4221
+
4222
+ var i;
4223
+
4224
+ if (_h5IntervalTimer !== null && !_h5TimerCount) {
4225
+
4226
+ // no active timers, stop polling interval.
4227
+
4228
+ window.clearInterval(_h5IntervalTimer);
4229
+
4230
+ _h5IntervalTimer = null;
4231
+
4232
+ return false;
4233
+
4234
+ }
4235
+
4236
+ // check all HTML5 sounds with timers
4237
+
4238
+ for (i = _s.soundIDs.length-1; i >= 0; i--) {
4239
+
4240
+ if (_s.sounds[_s.soundIDs[i]].isHTML5 && _s.sounds[_s.soundIDs[i]]._hasTimer) {
4241
+
4242
+ _s.sounds[_s.soundIDs[i]]._onTimer();
4243
+
4244
+ }
4245
+
4246
+ }
4247
+
4248
+ };
4249
+
4250
+ _catchError = function(options) {
4251
+
4252
+ options = (typeof options !== 'undefined' ? options : {});
4253
+
4254
+ if (_s.onerror instanceof Function) {
4255
+ _s.onerror.apply(_win, [{type:(typeof options.type !== 'undefined' ? options.type : null)}]);
4256
+ }
4257
+
4258
+ if (typeof options.fatal !== 'undefined' && options.fatal) {
4259
+ _s.disable();
4260
+ }
4261
+
4262
+ };
4263
+
4264
+ _badSafariFix = function() {
4265
+
4266
+ // special case: "bad" Safari (OS X 10.3 - 10.7) must fall back to flash for MP3/MP4
4267
+ if (!_isBadSafari || !_detectFlash()) {
4268
+ // doesn't apply
4269
+ return false;
4270
+ }
4271
+
4272
+ var aF = _s.audioFormats, i, item;
4273
+
4274
+ for (item in aF) {
4275
+ if (aF.hasOwnProperty(item)) {
4276
+ if (item === 'mp3' || item === 'mp4') {
4277
+ _s._wD(_sm+': Using flash fallback for '+item+' format');
4278
+ _s.html5[item] = false;
4279
+ // assign result to related formats, too
4280
+ if (aF[item] && aF[item].related) {
4281
+ for (i = aF[item].related.length-1; i >= 0; i--) {
4282
+ _s.html5[aF[item].related[i]] = false;
4283
+ }
4284
+ }
4285
+ }
4286
+ }
4287
+ }
4288
+
4289
+ };
4290
+
4291
+ /**
4292
+ * Pseudo-private flash/ExternalInterface methods
4293
+ * ----------------------------------------------
4294
+ */
4295
+
4296
+ this._setSandboxType = function(sandboxType) {
4297
+
4298
+ // <d>
4299
+ var sb = _s.sandbox;
4300
+
4301
+ sb.type = sandboxType;
4302
+ sb.description = sb.types[(typeof sb.types[sandboxType] !== 'undefined'?sandboxType:'unknown')];
4303
+
4304
+ _s._wD('Flash security sandbox type: ' + sb.type);
4305
+
4306
+ if (sb.type === 'localWithFile') {
4307
+
4308
+ sb.noRemote = true;
4309
+ sb.noLocal = false;
4310
+ _wDS('secNote', 2);
4311
+
4312
+ } else if (sb.type === 'localWithNetwork') {
4313
+
4314
+ sb.noRemote = false;
4315
+ sb.noLocal = true;
4316
+
4317
+ } else if (sb.type === 'localTrusted') {
4318
+
4319
+ sb.noRemote = false;
4320
+ sb.noLocal = false;
4321
+
4322
+ }
4323
+ // </d>
4324
+
4325
+ };
4326
+
4327
+ this._externalInterfaceOK = function(flashDate, swfVersion) {
4328
+
4329
+ // flash callback confirming flash loaded, EI working etc.
4330
+ // flashDate = approx. timing/delay info for JS/flash bridge
4331
+ // swfVersion: SWF build string
4332
+
4333
+ if (_s.swfLoaded) {
4334
+ return false;
4335
+ }
4336
+
4337
+ var e, eiTime = new Date().getTime();
4338
+
4339
+ _s._wD(_smc+'externalInterfaceOK()' + (flashDate?' (~' + (eiTime - flashDate) + ' ms)':''));
4340
+ _debugTS('swf', true);
4341
+ _debugTS('flashtojs', true);
4342
+ _s.swfLoaded = true;
4343
+ _tryInitOnFocus = false;
4344
+
4345
+ if (_isBadSafari) {
4346
+ _badSafariFix();
4347
+ }
4348
+
4349
+ // complain if JS + SWF build/version strings don't match, excluding +DEV builds
4350
+ // <d>
4351
+ if (!swfVersion || swfVersion.replace(/\+dev/i,'') !== _s.versionNumber.replace(/\+dev/i, '')) {
4352
+
4353
+ e = _sm + ': Fatal: JavaScript file build "' + _s.versionNumber + '" does not match Flash SWF build "' + swfVersion + '" at ' + _s.url + '. Ensure both are up-to-date.';
4354
+
4355
+ // escape flash -> JS stack so this error fires in window.
4356
+ setTimeout(function versionMismatch() {
4357
+ throw new Error(e);
4358
+ }, 0);
4359
+
4360
+ // exit, init will fail with timeout
4361
+ return false;
4362
+
4363
+ }
4364
+ // </d>
4365
+
4366
+ if (_isIE) {
4367
+ // IE needs a timeout OR delay until window.onload - may need TODO: investigating
4368
+ setTimeout(_init, 100);
4369
+ } else {
4370
+ _init();
4371
+ }
4372
+
4373
+ };
4374
+
4375
+ /**
4376
+ * Private initialization helpers
4377
+ * ------------------------------
4378
+ */
4379
+
4380
+ _createMovie = function(smID, smURL) {
4381
+
4382
+ if (_didAppend && _appendSuccess) {
4383
+ // ignore if already succeeded
4384
+ return false;
4385
+ }
4386
+
4387
+ function _initMsg() {
4388
+ _s._wD('-- SoundManager 2 ' + _s.version + (!_s.html5Only && _s.useHTML5Audio?(_s.hasHTML5?' + HTML5 audio':', no HTML5 audio support'):'') + (!_s.html5Only ? (_s.useHighPerformance?', high performance mode, ':', ') + (( _s.flashPollingInterval ? 'custom (' + _s.flashPollingInterval + 'ms)' : 'normal') + ' polling') + (_s.wmode?', wmode: ' + _s.wmode:'') + (_s.debugFlash?', flash debug mode':'') + (_s.useFlashBlock?', flashBlock mode':'') : '') + ' --', 1);
4389
+ }
4390
+
4391
+ if (_s.html5Only) {
4392
+
4393
+ // 100% HTML5 mode
4394
+ _setVersionInfo();
4395
+
4396
+ _initMsg();
4397
+ _s.oMC = _id(_s.movieID);
4398
+ _init();
4399
+
4400
+ // prevent multiple init attempts
4401
+ _didAppend = true;
4402
+
4403
+ _appendSuccess = true;
4404
+
4405
+ return false;
4406
+
4407
+ }
4408
+
4409
+ // flash path
4410
+ var remoteURL = (smURL || _s.url),
4411
+ localURL = (_s.altURL || remoteURL),
4412
+ swfTitle = 'JS/Flash audio component (SoundManager 2)',
4413
+ oEmbed, oMovie, oTarget = _getDocument(), tmp, movieHTML, oEl, extraClass = _getSWFCSS(),
4414
+ s, x, sClass, side = null, isRTL = null,
4415
+ html = _doc.getElementsByTagName('html')[0];
4416
+
4417
+ isRTL = (html && html.dir && html.dir.match(/rtl/i));
4418
+ smID = (typeof smID === 'undefined'?_s.id:smID);
4419
+
4420
+ function param(name, value) {
4421
+ return '<param name="'+name+'" value="'+value+'" />';
4422
+ }
4423
+
4424
+ // safety check for legacy (change to Flash 9 URL)
4425
+ _setVersionInfo();
4426
+ _s.url = _normalizeMovieURL(_overHTTP?remoteURL:localURL);
4427
+ smURL = _s.url;
4428
+
4429
+ _s.wmode = (!_s.wmode && _s.useHighPerformance ? 'transparent' : _s.wmode);
4430
+
4431
+ if (_s.wmode !== null && (_ua.match(/msie 8/i) || (!_isIE && !_s.useHighPerformance)) && navigator.platform.match(/win32|win64/i)) {
4432
+ /**
4433
+ * extra-special case: movie doesn't load until scrolled into view when using wmode = anything but 'window' here
4434
+ * does not apply when using high performance (position:fixed means on-screen), OR infinite flash load timeout
4435
+ * wmode breaks IE 8 on Vista + Win7 too in some cases, as of January 2011 (?)
4436
+ */
4437
+ _wDS('spcWmode');
4438
+ _s.wmode = null;
4439
+ }
4440
+
4441
+ oEmbed = {
4442
+ 'name': smID,
4443
+ 'id': smID,
4444
+ 'src': smURL,
4445
+ 'quality': 'high',
4446
+ 'allowScriptAccess': _s.allowScriptAccess,
4447
+ 'bgcolor': _s.bgColor,
4448
+ 'pluginspage': _http+'www.macromedia.com/go/getflashplayer',
4449
+ 'title': swfTitle,
4450
+ 'type': 'application/x-shockwave-flash',
4451
+ 'wmode': _s.wmode,
4452
+ // http://help.adobe.com/en_US/as3/mobile/WS4bebcd66a74275c36cfb8137124318eebc6-7ffd.html
4453
+ 'hasPriority': 'true'
4454
+ };
4455
+
4456
+ if (side !== null) {
4457
+ // don't specify width/height if null.
4458
+ oEmbed.width = side;
4459
+ oEmbed.height = side;
4460
+ }
4461
+
4462
+ if (_s.debugFlash) {
4463
+ oEmbed.FlashVars = 'debug=1';
4464
+ }
4465
+
4466
+ if (!_s.wmode) {
4467
+ // don't write empty attribute
4468
+ delete oEmbed.wmode;
4469
+ }
4470
+
4471
+ if (_isIE) {
4472
+
4473
+ // IE is "special".
4474
+ oMovie = _doc.createElement('div');
4475
+ movieHTML = [
4476
+ '<object id="' + smID + '" data="' + smURL + '" type="' + oEmbed.type + '" title="' + oEmbed.title +'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="' + _http+'download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" width="' + oEmbed.width + '" height="' + oEmbed.height + '">',
4477
+ param('movie', smURL),
4478
+ param('AllowScriptAccess', _s.allowScriptAccess),
4479
+ param('quality', oEmbed.quality),
4480
+ (_s.wmode? param('wmode', _s.wmode): ''),
4481
+ param('bgcolor', _s.bgColor),
4482
+ param('hasPriority', 'true'),
4483
+ (_s.debugFlash ? param('FlashVars', oEmbed.FlashVars) : ''),
4484
+ '</object>'
4485
+ ].join('');
4486
+
4487
+ } else {
4488
+
4489
+ oMovie = _doc.createElement('embed');
4490
+ for (tmp in oEmbed) {
4491
+ if (oEmbed.hasOwnProperty(tmp)) {
4492
+ oMovie.setAttribute(tmp, oEmbed[tmp]);
4493
+ }
4494
+ }
4495
+
4496
+ }
4497
+
4498
+ _initDebug();
4499
+ extraClass = _getSWFCSS();
4500
+ oTarget = _getDocument();
4501
+
4502
+ if (oTarget) {
4503
+
4504
+ _s.oMC = (_id(_s.movieID) || _doc.createElement('div'));
4505
+
4506
+ if (!_s.oMC.id) {
4507
+
4508
+ _s.oMC.id = _s.movieID;
4509
+ _s.oMC.className = _swfCSS.swfDefault + ' ' + extraClass;
4510
+ s = null;
4511
+ oEl = null;
4512
+
4513
+ if (!_s.useFlashBlock) {
4514
+ if (_s.useHighPerformance) {
4515
+ // on-screen at all times
4516
+ s = {
4517
+ 'position': 'fixed',
4518
+ 'width': '8px',
4519
+ 'height': '8px',
4520
+ // >= 6px for flash to run fast, >= 8px to start up under Firefox/win32 in some cases. odd? yes.
4521
+ 'bottom': '0px',
4522
+ 'left': '0px',
4523
+ 'overflow': 'hidden'
4524
+ };
4525
+ } else {
4526
+ // hide off-screen, lower priority
4527
+ s = {
4528
+ 'position': 'absolute',
4529
+ 'width': '6px',
4530
+ 'height': '6px',
4531
+ 'top': '-9999px',
4532
+ 'left': '-9999px'
4533
+ };
4534
+ if (isRTL) {
4535
+ s.left = Math.abs(parseInt(s.left,10))+'px';
4536
+ }
4537
+ }
4538
+ }
4539
+
4540
+ if (_isWebkit) {
4541
+ // soundcloud-reported render/crash fix, safari 5
4542
+ _s.oMC.style.zIndex = 10000;
4543
+ }
4544
+
4545
+ if (!_s.debugFlash) {
4546
+ for (x in s) {
4547
+ if (s.hasOwnProperty(x)) {
4548
+ _s.oMC.style[x] = s[x];
4549
+ }
4550
+ }
4551
+ }
4552
+
4553
+ try {
4554
+ if (!_isIE) {
4555
+ _s.oMC.appendChild(oMovie);
4556
+ }
4557
+ oTarget.appendChild(_s.oMC);
4558
+ if (_isIE) {
4559
+ oEl = _s.oMC.appendChild(_doc.createElement('div'));
4560
+ oEl.className = _swfCSS.swfBox;
4561
+ oEl.innerHTML = movieHTML;
4562
+ }
4563
+ _appendSuccess = true;
4564
+ } catch(e) {
4565
+ throw new Error(_str('domError')+' \n'+e.toString());
4566
+ }
4567
+
4568
+ } else {
4569
+
4570
+ // SM2 container is already in the document (eg. flashblock use case)
4571
+ sClass = _s.oMC.className;
4572
+ _s.oMC.className = (sClass?sClass+' ':_swfCSS.swfDefault) + (extraClass?' '+extraClass:'');
4573
+ _s.oMC.appendChild(oMovie);
4574
+ if (_isIE) {
4575
+ oEl = _s.oMC.appendChild(_doc.createElement('div'));
4576
+ oEl.className = _swfCSS.swfBox;
4577
+ oEl.innerHTML = movieHTML;
4578
+ }
4579
+ _appendSuccess = true;
4580
+
4581
+ }
4582
+
4583
+ }
4584
+
4585
+ _didAppend = true;
4586
+ _initMsg();
4587
+ _s._wD(_smc+'createMovie(): Trying to load ' + smURL + (!_overHTTP && _s.altURL?' (alternate URL)':''), 1);
4588
+
4589
+ return true;
4590
+
4591
+ };
4592
+
4593
+ _initMovie = function() {
4594
+
4595
+ if (_s.html5Only) {
4596
+ _createMovie();
4597
+ return false;
4598
+ }
4599
+
4600
+ // attempt to get, or create, movie
4601
+ // may already exist
4602
+ if (_flash) {
4603
+ return false;
4604
+ }
4605
+
4606
+ // inline markup case
4607
+ _flash = _s.getMovie(_s.id);
4608
+
4609
+ if (!_flash) {
4610
+ if (!_oRemoved) {
4611
+ // try to create
4612
+ _createMovie(_s.id, _s.url);
4613
+ } else {
4614
+ // try to re-append removed movie after reboot()
4615
+ if (!_isIE) {
4616
+ _s.oMC.appendChild(_oRemoved);
4617
+ } else {
4618
+ _s.oMC.innerHTML = _oRemovedHTML;
4619
+ }
4620
+ _oRemoved = null;
4621
+ _didAppend = true;
4622
+ }
4623
+ _flash = _s.getMovie(_s.id);
4624
+ }
4625
+
4626
+ // <d>
4627
+ if (_flash) {
4628
+ _wDS('waitEI');
4629
+ }
4630
+ // </d>
4631
+
4632
+ if (_s.oninitmovie instanceof Function) {
4633
+ setTimeout(_s.oninitmovie, 1);
4634
+ }
4635
+
4636
+ return true;
4637
+
4638
+ };
4639
+
4640
+ _delayWaitForEI = function() {
4641
+
4642
+ setTimeout(_waitForEI, 1000);
4643
+
4644
+ };
4645
+
4646
+ _waitForEI = function() {
4647
+
4648
+ if (_waitingForEI) {
4649
+ return false;
4650
+ }
4651
+
4652
+ _waitingForEI = true;
4653
+ _event.remove(_win, 'load', _delayWaitForEI);
4654
+
4655
+ if (_tryInitOnFocus && !_isFocused) {
4656
+ // giant Safari 3.1 hack - assume mousemove = focus given lack of focus event
4657
+ _wDS('waitFocus');
4658
+ return false;
4659
+ }
4660
+
4661
+ var p;
4662
+ if (!_didInit) {
4663
+ p = _s.getMoviePercent();
4664
+ _s._wD(_str('waitImpatient', (p === 100?' (SWF loaded)':(p > 0?' (SWF ' + p + '% loaded)':''))));
4665
+ }
4666
+
4667
+ setTimeout(function() {
4668
+
4669
+ p = _s.getMoviePercent();
4670
+
4671
+ // <d>
4672
+ if (!_didInit) {
4673
+ _s._wD(_sm + ': No Flash response within expected time.\nLikely causes: ' + (p === 0?'Loading ' + _s.movieURL + ' may have failed (and/or Flash ' + _fV + '+ not present?), ':'') + 'Flash blocked or JS-Flash security error.' + (_s.debugFlash?' ' + _str('checkSWF'):''), 2);
4674
+ if (!_overHTTP && p) {
4675
+ _wDS('localFail', 2);
4676
+ if (!_s.debugFlash) {
4677
+ _wDS('tryDebug', 2);
4678
+ }
4679
+ }
4680
+ if (p === 0) {
4681
+ // if 0 (not null), probably a 404.
4682
+ _s._wD(_str('swf404', _s.url));
4683
+ }
4684
+ _debugTS('flashtojs', false, ': Timed out' + _overHTTP?' (Check flash security or flash blockers)':' (No plugin/missing SWF?)');
4685
+ }
4686
+ // </d>
4687
+
4688
+ // give up / time-out, depending
4689
+
4690
+ if (!_didInit && _okToDisable) {
4691
+ if (p === null) {
4692
+ // SWF failed. Maybe blocked.
4693
+ if (_s.useFlashBlock || _s.flashLoadTimeout === 0) {
4694
+ if (_s.useFlashBlock) {
4695
+ _flashBlockHandler();
4696
+ }
4697
+ _wDS('waitForever');
4698
+ } else {
4699
+ // old SM2 behaviour, simply fail
4700
+ _failSafely(true);
4701
+ }
4702
+ } else {
4703
+ // flash loaded? Shouldn't be a blocking issue, then.
4704
+ if (_s.flashLoadTimeout === 0) {
4705
+ _wDS('waitForever');
4706
+ } else {
4707
+ _failSafely(true);
4708
+ }
4709
+ }
4710
+ }
4711
+
4712
+ }, _s.flashLoadTimeout);
4713
+
4714
+ };
4715
+
4716
+ _handleFocus = function() {
4717
+
4718
+ function cleanup() {
4719
+ _event.remove(_win, 'focus', _handleFocus);
4720
+ _event.remove(_win, 'load', _handleFocus);
4721
+ }
4722
+
4723
+ if (_isFocused || !_tryInitOnFocus) {
4724
+ cleanup();
4725
+ return true;
4726
+ }
4727
+
4728
+ _okToDisable = true;
4729
+ _isFocused = true;
4730
+ _s._wD(_smc+'handleFocus()');
4731
+
4732
+ if (_isSafari && _tryInitOnFocus) {
4733
+ _event.remove(_win, 'mousemove', _handleFocus);
4734
+ }
4735
+
4736
+ // allow init to restart
4737
+ _waitingForEI = false;
4738
+
4739
+ cleanup();
4740
+ return true;
4741
+
4742
+ };
4743
+
4744
+ _showSupport = function() {
4745
+
4746
+ var item, tests = [];
4747
+
4748
+ if (_s.useHTML5Audio && _s.hasHTML5) {
4749
+ for (item in _s.audioFormats) {
4750
+ if (_s.audioFormats.hasOwnProperty(item)) {
4751
+ tests.push(item + ': ' + _s.html5[item] + (!_s.html5[item] && _hasFlash && _s.flash[item] ? ' (using flash)' : (_s.preferFlash && _s.flash[item] && _hasFlash ? ' (preferring flash)': (!_s.html5[item] ? ' (' + (_s.audioFormats[item].required ? 'required, ':'') + 'and no flash support)' : ''))));
4752
+ }
4753
+ }
4754
+ _s._wD('-- SoundManager 2: HTML5 support tests ('+_s.html5Test+'): '+tests.join(', ')+' --',1);
4755
+ }
4756
+
4757
+ };
4758
+
4759
+ _initComplete = function(bNoDisable) {
4760
+
4761
+ if (_didInit) {
4762
+ return false;
4763
+ }
4764
+
4765
+ if (_s.html5Only) {
4766
+ // all good.
4767
+ _s._wD('-- SoundManager 2: loaded --');
4768
+ _didInit = true;
4769
+ _initUserOnload();
4770
+ _debugTS('onload', true);
4771
+ return true;
4772
+ }
4773
+
4774
+ var wasTimeout = (_s.useFlashBlock && _s.flashLoadTimeout && !_s.getMoviePercent()),
4775
+ error;
4776
+
4777
+ if (!wasTimeout) {
4778
+ _didInit = true;
4779
+ if (_disabled) {
4780
+ error = {type: (!_hasFlash && _needsFlash ? 'NO_FLASH' : 'INIT_TIMEOUT')};
4781
+ }
4782
+ }
4783
+
4784
+ _s._wD('-- SoundManager 2 ' + (_disabled?'failed to load':'loaded') + ' (' + (_disabled?'security/load error':'OK') + ') --', 1);
4785
+
4786
+ if (_disabled || bNoDisable) {
4787
+ if (_s.useFlashBlock && _s.oMC) {
4788
+ _s.oMC.className = _getSWFCSS() + ' ' + (_s.getMoviePercent() === null?_swfCSS.swfTimedout:_swfCSS.swfError);
4789
+ }
4790
+ _processOnEvents({type:'ontimeout', error:error});
4791
+ _debugTS('onload', false);
4792
+ _catchError(error);
4793
+ return false;
4794
+ } else {
4795
+ _debugTS('onload', true);
4796
+ }
4797
+
4798
+ if (_s.waitForWindowLoad && !_windowLoaded) {
4799
+ _wDS('waitOnload');
4800
+ _event.add(_win, 'load', _initUserOnload);
4801
+ return false;
4802
+ } else {
4803
+ // <d>
4804
+ if (_s.waitForWindowLoad && _windowLoaded) {
4805
+ _wDS('docLoaded');
4806
+ }
4807
+ // </d>
4808
+ _initUserOnload();
4809
+ }
4810
+
4811
+ return true;
4812
+
4813
+ };
4814
+
4815
+ _init = function() {
4816
+
4817
+ _wDS('init');
4818
+
4819
+ // called after onload()
4820
+
4821
+ if (_didInit) {
4822
+ _wDS('didInit');
4823
+ return false;
4824
+ }
4825
+
4826
+ function _cleanup() {
4827
+ _event.remove(_win, 'load', _s.beginDelayedInit);
4828
+ }
4829
+
4830
+ if (_s.html5Only) {
4831
+ if (!_didInit) {
4832
+ // we don't need no steenking flash!
4833
+ _cleanup();
4834
+ _s.enabled = true;
4835
+ _initComplete();
4836
+ }
4837
+ return true;
4838
+ }
4839
+
4840
+ // flash path
4841
+ _initMovie();
4842
+
4843
+ try {
4844
+
4845
+ _wDS('flashJS');
4846
+
4847
+ // attempt to talk to Flash
4848
+ _flash._externalInterfaceTest(false);
4849
+
4850
+ // apply user-specified polling interval, OR, if "high performance" set, faster vs. default polling
4851
+ // (determines frequency of whileloading/whileplaying callbacks, effectively driving UI framerates)
4852
+ _setPolling(true, (_s.flashPollingInterval || (_s.useHighPerformance ? 10 : 50)));
4853
+
4854
+ if (!_s.debugMode) {
4855
+ // stop the SWF from making debug output calls to JS
4856
+ _flash._disableDebug();
4857
+ }
4858
+
4859
+ _s.enabled = true;
4860
+ _debugTS('jstoflash', true);
4861
+
4862
+ if (!_s.html5Only) {
4863
+ // prevent browser from showing cached page state (or rather, restoring "suspended" page state) via back button, because flash may be dead
4864
+ // http://www.webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/
4865
+ _event.add(_win, 'unload', _doNothing);
4866
+ }
4867
+
4868
+ } catch(e) {
4869
+
4870
+ _s._wD('js/flash exception: ' + e.toString());
4871
+ _debugTS('jstoflash', false);
4872
+ _catchError({type:'JS_TO_FLASH_EXCEPTION', fatal:true});
4873
+ // don't disable, for reboot()
4874
+ _failSafely(true);
4875
+ _initComplete();
4876
+
4877
+ return false;
4878
+
4879
+ }
4880
+
4881
+ _initComplete();
4882
+
4883
+ // disconnect events
4884
+ _cleanup();
4885
+
4886
+ return true;
4887
+
4888
+ };
4889
+
4890
+ _domContentLoaded = function() {
4891
+
4892
+ if (_didDCLoaded) {
4893
+ return false;
4894
+ }
4895
+
4896
+ _didDCLoaded = true;
4897
+ _initDebug();
4898
+
4899
+ /**
4900
+ * Temporary feature: allow force of HTML5 via URL params: sm2-usehtml5audio=0 or 1
4901
+ * Ditto for sm2-preferFlash, too.
4902
+ */
4903
+ // <d>
4904
+ (function(){
4905
+
4906
+ var a = 'sm2-usehtml5audio=', l = _wl.toLowerCase(), b = null,
4907
+ a2 = 'sm2-preferflash=', b2 = null, hasCon = (typeof console !== 'undefined' && typeof console.log !== 'undefined');
4908
+
4909
+ if (l.indexOf(a) !== -1) {
4910
+ b = (l.charAt(l.indexOf(a)+a.length) === '1');
4911
+ if (hasCon) {
4912
+ console.log((b?'Enabling ':'Disabling ')+'useHTML5Audio via URL parameter');
4913
+ }
4914
+ _s.useHTML5Audio = b;
4915
+ }
4916
+
4917
+ if (l.indexOf(a2) !== -1) {
4918
+ b2 = (l.charAt(l.indexOf(a2)+a2.length) === '1');
4919
+ if (hasCon) {
4920
+ console.log((b2?'Enabling ':'Disabling ')+'preferFlash via URL parameter');
4921
+ }
4922
+ _s.preferFlash = b2;
4923
+ }
4924
+
4925
+ }());
4926
+ // </d>
4927
+
4928
+ if (!_hasFlash && _s.hasHTML5) {
4929
+ _s._wD('SoundManager: No Flash detected'+(!_s.useHTML5Audio?', enabling HTML5.':'. Trying HTML5-only mode.'));
4930
+ _s.useHTML5Audio = true;
4931
+ // make sure we aren't preferring flash, either
4932
+ // TODO: preferFlash should not matter if flash is not installed. Currently, stuff breaks without the below tweak.
4933
+ _s.preferFlash = false;
4934
+ }
4935
+
4936
+ _testHTML5();
4937
+ _s.html5.usingFlash = _featureCheck();
4938
+ _needsFlash = _s.html5.usingFlash;
4939
+ _showSupport();
4940
+
4941
+ if (!_hasFlash && _needsFlash) {
4942
+ _s._wD('SoundManager: Fatal error: Flash is needed to play some required formats, but is not available.');
4943
+ // TODO: Fatal here vs. timeout approach, etc.
4944
+ // hack: fail sooner.
4945
+ _s.flashLoadTimeout = 1;
4946
+ }
4947
+
4948
+ if (_doc.removeEventListener) {
4949
+ _doc.removeEventListener('DOMContentLoaded', _domContentLoaded, false);
4950
+ }
4951
+
4952
+ _initMovie();
4953
+ return true;
4954
+
4955
+ };
4956
+
4957
+ _domContentLoadedIE = function() {
4958
+
4959
+ if (_doc.readyState === 'complete') {
4960
+ _domContentLoaded();
4961
+ _doc.detachEvent('onreadystatechange', _domContentLoadedIE);
4962
+ }
4963
+
4964
+ return true;
4965
+
4966
+ };
4967
+
4968
+ _winOnLoad = function() {
4969
+ // catch edge case of _initComplete() firing after window.load()
4970
+ _windowLoaded = true;
4971
+ _event.remove(_win, 'load', _winOnLoad);
4972
+ };
4973
+
4974
+ // sniff up-front
4975
+ _detectFlash();
4976
+
4977
+ // focus and window load, init (primarily flash-driven)
4978
+ _event.add(_win, 'focus', _handleFocus);
4979
+ _event.add(_win, 'load', _handleFocus);
4980
+ _event.add(_win, 'load', _delayWaitForEI);
4981
+ _event.add(_win, 'load', _winOnLoad);
4982
+
4983
+
4984
+ if (_isSafari && _tryInitOnFocus) {
4985
+ // massive Safari 3.1 focus detection hack
4986
+ _event.add(_win, 'mousemove', _handleFocus);
4987
+ }
4988
+
4989
+ if (_doc.addEventListener) {
4990
+
4991
+ _doc.addEventListener('DOMContentLoaded', _domContentLoaded, false);
4992
+
4993
+ } else if (_doc.attachEvent) {
4994
+
4995
+ _doc.attachEvent('onreadystatechange', _domContentLoadedIE);
4996
+
4997
+ } else {
4998
+
4999
+ // no add/attachevent support - safe to assume no JS -> Flash either
5000
+ _debugTS('onload', false);
5001
+ _catchError({type:'NO_DOM2_EVENTS', fatal:true});
5002
+
5003
+ }
5004
+
5005
+ if (_doc.readyState === 'complete') {
5006
+ // DOMReady has already happened.
5007
+ setTimeout(_domContentLoaded,100);
5008
+ }
5009
+
5010
+ } // SoundManager()
5011
+
5012
+ // SM2_DEFER details: http://www.schillmania.com/projects/soundmanager2/doc/getstarted/#lazy-loading
5013
+
5014
+ if (typeof SM2_DEFER === 'undefined' || !SM2_DEFER) {
5015
+ soundManager = new SoundManager();
5016
+ }
5017
+
5018
+ /**
5019
+ * SoundManager public interfaces
5020
+ * ------------------------------
5021
+ */
5022
+
5023
+ window.SoundManager = SoundManager; // constructor
5024
+ window.soundManager = soundManager; // public API, flash callbacks etc.
5025
+
5026
+ }(window));