-
Notifications
You must be signed in to change notification settings - Fork 490
/
Copy pathSound.js
1279 lines (1087 loc) · 37.2 KB
/
Sound.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @author Richard Davey <[email protected]>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Sound class constructor.
*
* @class Phaser.Sound
* @constructor
* @param {Phaser.Game} game - Reference to the current game instance.
* @param {string} key - Asset key for the sound.
* @param {number} [volume=1] - Default value for the volume, between 0 and 1.
* @param {boolean} [loop=false] - Whether or not the sound will loop.
*/
Phaser.Sound = function (game, key, volume, loop, connect)
{
if (volume === undefined) { volume = 1; }
if (loop === undefined) { loop = false; }
if (connect === undefined) { connect = game.sound.connectToMaster; }
/**
* A reference to the currently running Game.
* @property {Phaser.Game} game
*/
this.game = game;
/**
* @property {string} name - Name of the sound.
*/
this.name = key;
/**
* @property {string} key - Asset key for the sound.
*/
this.key = key;
/**
* @property {boolean} loop - Whether or not the sound or current sound marker will loop.
*/
this.loop = loop;
/**
* @property {object} markers - The sound markers.
*/
this.markers = {};
/**
* @property {AudioContext} context - Reference to the AudioContext instance.
*/
this.context = null;
/**
* @property {boolean} autoplay - Whether the sound should start automatically.
*/
this.autoplay = false;
/**
* @property {number} totalDuration - The total duration of the sound in seconds.
*/
this.totalDuration = 0;
/**
* @property {number} startTime - The time the sound starts playing, in game-time coordinates (ms).
* @default
*/
this.startTime = 0;
/**
* @property {number} currentTime - The current time of sound playback in ms.
*/
this.currentTime = 0;
/**
* @property {number} duration - The duration of the current sound marker in seconds.
*/
this.duration = 0;
/**
* @property {number} durationMS - The duration of the current sound marker in ms.
*/
this.durationMS = 0;
/**
* @property {number} position - The position of the current sound marker in seconds.
*/
this.position = 0;
/**
* @property {number} stopTime - The time the sound stopped in ms.
*/
this.stopTime = 0;
/**
* @property {boolean} paused - Whether the sound is paused.
* @default
*/
this.paused = false;
/**
* @property {number} pausedPosition - The position the sound had reached when it was paused in ms.
*/
this.pausedPosition = 0;
/**
* @property {number} pausedTime - The clock time (ms) at which the sound was paused.
*/
this.pausedTime = 0;
/**
* @property {boolean} isPlaying - Whether the sound is currently playing.
* @default
*/
this.isPlaying = false;
/**
* @property {string} currentMarker - The string ID of the currently playing marker, if any.
* @default
*/
this.currentMarker = '';
/**
* @property {Phaser.Tween} fadeTween - The tween that fades the audio, set via Sound.fadeIn and Sound.fadeOut.
*/
this.fadeTween = null;
/**
* @property {boolean} pendingPlayback - Playback is pending (delayed) because the audio isn't decoded or is touch-locked.
* @readonly
*/
this.pendingPlayback = false;
/**
* @property {boolean} override - When playing this sound, always start from the beginning.
* @default
*/
this.override = false;
/**
* @property {boolean} allowMultiple - This will allow you to have multiple instances of this Sound playing at once. This is only useful when running under Web Audio, and we recommend you implement a local pooling system to not flood the sound channels.
* @default
*/
this.allowMultiple = false;
/**
* @property {boolean} playOnce - Marks the Sound for deletion from SoundManager after playing once. Useful for playing several identical sounds overlapping without flooding the sound channel
* @default
*/
this.playOnce = false;
/**
* @property {boolean} usingWebAudio - Whether this sound is being played with Web Audio.
* @readonly
*/
this.usingWebAudio = this.game.sound.usingWebAudio;
/**
* @property {boolean} usingAudioTag - Whether this sound is being played via the Audio tag.
* @readonly
*/
this.usingAudioTag = this.game.sound.usingAudioTag;
/**
* @property {object} externalNode - If defined this Sound won't connect to the SoundManager master gain node, but will instead connect to externalNode.
*/
this.externalNode = null;
/**
* @property {object} masterGainNode - The master gain node in a Web Audio system.
*/
this.masterGainNode = null;
/**
* @property {object} gainNode - The gain node in a Web Audio system.
*/
this.gainNode = null;
/**
* @property {AudioBufferSourceNode|HTMLAudioElement} _sound - The audio source.
* @private
*/
this._sound = null;
/**
* @property {object} _globalVolume - Internal var for keeping track of global volume when using AudioTag
* @private
*/
this._globalVolume = 1;
/**
* @property {boolean} _markedToDelete - When audio stops, disconnect Web Audio nodes.
* @private
*/
this._markedToDelete = false;
/**
* @property {boolean} _pendingStart - play() was called but waiting for playback. Audio Tag only, and not used for sound markers. Cleared in update() once playback starts.
* @private
*/
this._pendingStart = false;
/**
* @property {boolean} _removeFromSoundManager - When audio stops, remove it from the Sound Manager and destroy it.
* @private
*/
this._removeFromSoundManager = false;
/**
* @property {number} _sourceId - For debugging Web Audio sources.
* @private
*/
this._sourceId = 0;
if (this.usingWebAudio)
{
this.context = this.game.sound.context;
this.masterGainNode = this.game.sound.masterGain;
if (this.context.createGain === undefined)
{
this.gainNode = this.context.createGainNode();
}
else
{
this.gainNode = this.context.createGain();
}
this.gainNode.gain.value = volume;
if (connect)
{
this.gainNode.connect(this.masterGainNode);
}
}
else if (this.usingAudioTag)
{
if (this.game.cache.getSound(key) && this.game.cache.isSoundReady(key))
{
this._sound = this.game.cache.getSoundData(key);
this.totalDuration = 0;
if (this._sound.duration)
{
this.totalDuration = this._sound.duration;
}
}
else
{
this.game.cache.onSoundUnlock.add(this.soundHasUnlocked, this);
}
}
/**
* @property {Phaser.Signal} onDecoded - The onDecoded event is dispatched when the sound has finished decoding (typically for mp3 files). It passes one argument, this sound.
*/
this.onDecoded = new Phaser.Signal();
/**
* @property {Phaser.Signal} onPlay - The onPlay event is dispatched each time this sound is played (but not looped). It passes one argument, this sound.
*/
this.onPlay = new Phaser.Signal();
/**
* @property {Phaser.Signal} onPause - The onPause event is dispatched when this sound is paused. It passes one argument, this sound.
*/
this.onPause = new Phaser.Signal();
/**
* @property {Phaser.Signal} onResume - The onResume event is dispatched when this sound is resumed from a paused state. It passes one argument, this sound.
*/
this.onResume = new Phaser.Signal();
/**
* @property {Phaser.Signal} onLoop - The onLoop event is dispatched when this sound loops during playback. It passes one argument, this sound.
*/
this.onLoop = new Phaser.Signal();
/**
* @property {Phaser.Signal} onStop - The onStop event is dispatched when this sound stops playback or when a non-looping marker completes. It passes two arguments: this sound and any {@link #currentMarker marker} that was playing.
*/
this.onStop = new Phaser.Signal();
/**
* @property {Phaser.Signal} onMute - The onMute event is dispatched when this sound is muted. It passes one argument, this sound.
*/
this.onMute = new Phaser.Signal();
/**
* @property {Phaser.Signal} onMarkerComplete - The onMarkerComplete event is dispatched when a marker within this sound completes playback. It passes two arguments: the {@link #currentMarker} and this sound.
*/
this.onMarkerComplete = new Phaser.Signal();
/**
* @property {Phaser.Signal} onFadeComplete - The onFadeComplete event is dispatched when this sound finishes fading either in or out. It passes two arguments: this sound and its current {@link #volume}.
*/
this.onFadeComplete = new Phaser.Signal();
/**
* @property {number} _volume - The global audio volume. A value between 0 (silence) and 1 (full volume).
* @private
*/
this._volume = volume;
/**
* @property {any} _buffer - Decoded data buffer / Audio tag.
* @private
*/
this._buffer = null;
/**
* @property {boolean} _muted - Boolean indicating whether the sound is muted or not.
* @private
*/
this._muted = false;
/**
* @property {number} _tempMarker - Internal marker var.
* @private
*/
this._tempMarker = 0;
/**
* @property {number} _tempPosition - Internal marker var.
* @private
*/
this._tempPosition = 0;
/**
* @property {number} _tempVolume - Internal marker var.
* @private
*/
this._tempVolume = 0;
/**
* @property {number} _tempPause - Internal marker var.
* @private
*/
this._tempPause = 0;
/**
* @property {number} _muteVolume - Internal cache var.
* @private
*/
this._muteVolume = 0;
/**
* @property {boolean} _tempLoop - Internal cache var.
* @private
*/
this._tempLoop = 0;
/**
* @property {boolean} _paused - Was this sound paused via code or a game event?
* @private
*/
this._paused = false;
/**
* @property {boolean} _onDecodedEventDispatched - Was the onDecoded event dispatched?
* @private
*/
this._onDecodedEventDispatched = false;
};
Phaser.Sound.prototype = {
/**
* Called automatically when this sound is unlocked.
* @method Phaser.Sound#soundHasUnlocked
* @param {string} key - The Phaser.Cache key of the sound file to check for decoding.
* @protected
*/
soundHasUnlocked: function (key)
{
if (key === this.key)
{
this._sound = this.game.cache.getSoundData(this.key);
this.totalDuration = this._sound.duration;
}
},
/**
* Adds a marker into the current Sound. A marker is represented by a unique key and a start time and duration.
* This allows you to bundle multiple sounds together into a single audio file and use markers to jump between them for playback.
*
* @method Phaser.Sound#addMarker
* @param {string} name - A unique name for this marker, i.e. 'explosion', 'gunshot', etc.
* @param {number} start - The start point of this marker in the audio file, given in seconds. 2.5 = 2500ms, 0.5 = 500ms, etc.
* @param {number} [duration=1] - The duration of the marker in seconds. 2.5 = 2500ms, 0.5 = 500ms, etc.
* @param {number} [volume=1] - The volume the sound will play back at, between 0 (silent) and 1 (full volume).
* @param {boolean} [loop=false] - Sets if the sound will loop or not.
*/
addMarker: function (name, start, duration, volume, loop)
{
if (duration === undefined || duration === null) { duration = 1; }
if (volume === undefined || volume === null) { volume = 1; }
if (loop === undefined) { loop = false; }
this.markers[name] = {
name: name,
start: start,
stop: start + duration,
volume: volume,
duration: duration,
durationMS: duration * 1000,
loop: loop
};
},
/**
* Removes a marker from the sound.
* @method Phaser.Sound#removeMarker
* @param {string} name - The key of the marker to remove.
*/
removeMarker: function (name)
{
delete this.markers[name];
},
/**
* Called automatically by the AudioContext when the sound stops playing.
* Doesn't get called if the sound is set to loop or is a section of an Audio Sprite.
*
* @method Phaser.Sound#onEndedHandler
* @protected
*/
onEndedHandler: function ()
{
if (!this._sound)
{
// Probably destroyed?
return;
}
this._removeOnEndedHandler();
this.isPlaying = false;
this.currentTime = this.durationMS;
this.stop();
if (this.playOnce)
{
this._markedToDelete = true;
this._removeFromSoundManager = true;
}
if (this._markedToDelete)
{
this._disconnectSource();
if (this._removeFromSoundManager)
{
this.game.sound.remove(this);
}
else
{
this.markers = {};
this.context = null;
this._buffer = null;
this.externalNode = null;
this.onDecoded.dispose();
this.onPlay.dispose();
this.onPause.dispose();
this.onResume.dispose();
this.onLoop.dispose();
this.onStop.dispose();
this.onMute.dispose();
this.onMarkerComplete.dispose();
}
}
},
/**
* Called automatically by Phaser.SoundManager.
* @method Phaser.Sound#update
* @protected
*/
update: function ()
{
if (!this.game.cache.checkSoundKey(this.key))
{
this.destroy();
return;
}
if (this.isDecoded && !this._onDecodedEventDispatched)
{
this.onDecoded.dispatch(this);
this._onDecodedEventDispatched = true;
}
if (this.pendingPlayback && this.game.cache.isSoundReady(this.key))
{
this.pendingPlayback = false;
this.play(this._tempMarker, this._tempPosition, this._tempVolume, this._tempLoop);
}
var now = this.game.time.time;
if (this.isPlaying)
{
if (this._pendingStart)
{
var currentTime = this._sound.currentTime;
if (currentTime > ((this.paused ? this._tempPause : 0) || this.position || 0))
{
this._pendingStart = false;
this.startTime = now - (1000 * currentTime);
this.stopTime = this.startTime + this.durationMS;
}
else
{
// Still pending.
return;
}
}
this.currentTime = now - this.startTime;
if (this.currentTime >= this.durationMS)
{
if (this.usingWebAudio)
{
if (this.loop)
{
// won't work with markers, needs to reset the position
this.onLoop.dispatch(this);
// Gets reset by the play function
this.isPlaying = false;
if (this.currentMarker === '')
{
this.currentTime = 0;
this.startTime = now;
this.isPlaying = true; // play not called again in this case
}
else
{
this.onMarkerComplete.dispatch(this.currentMarker, this);
this.play(this.currentMarker, 0, this.volume, true, true, false);
}
}
else
{
// Stop if we're using an audio marker, otherwise we let onended handle it
if (this.currentMarker !== '')
{
this.stop();
}
}
}
else if (this.loop)
{
this.onLoop.dispatch(this);
if (this.currentMarker === '')
{
this.currentTime = 0;
this.startTime = now;
}
// Gets reset by the play function
this.isPlaying = false;
this.play(this.currentMarker, 0, this.volume, true, true, false);
}
else
{
this.stop();
}
}
}
},
/**
* Loops this entire sound. If you need to loop a section of it then use Sound.play and the marker and loop parameters.
*
* @method Phaser.Sound#loopFull
* @param {number} [volume=1] - Volume of the sound you want to play. If none is given it will use the volume given to the Sound when it was created (which defaults to 1 if none was specified).
* @return {Phaser.Sound} This sound instance.
*/
loopFull: function (volume)
{
return this.play(null, 0, volume, true);
},
/**
* Play this sound, or a marked section of it.
*
* @method Phaser.Sound#play
* @param {string} [marker=''] - If you want to play a marker then give the key here, otherwise leave blank to play the full sound.
* @param {number} [position=0] - The starting position to play the sound from - this is ignored if you provide a marker.
* @param {number} [volume=1] - Volume of the sound you want to play. If none is given it will use the volume given to the Sound when it was created (which defaults to 1 if none was specified).
* @param {boolean} [loop=false] - Loop when finished playing? If not using a marker / audio sprite the looping will be done via the WebAudio loop property, otherwise it's time based.
* @param {boolean} [forceRestart=true] - If the sound is already playing you can set forceRestart to restart it from the beginning.
* @param {boolean} [onPlay=true] - Dispatch the `onPlay` signal.
* @return {Phaser.Sound} This sound instance.
*/
play: function (marker, position, volume, loop, forceRestart, onPlay)
{
if (marker === undefined || marker === false || marker === null) { marker = ''; }
if (forceRestart === undefined) { forceRestart = true; }
if (onPlay === undefined) { onPlay = true; }
if (this.isPlaying && !this.allowMultiple && !forceRestart && !this.override)
{
// Use Restart instead
return this;
}
if (this._sound && this.isPlaying && !this.allowMultiple && (this.override || forceRestart))
{
if (this.usingWebAudio)
{
this._stopSourceAndDisconnect();
}
else if (this.usingAudioTag)
{
this._sound.pause();
this._sound.currentTime = 0;
}
this.isPlaying = false;
}
if (marker === '' && Object.keys(this.markers).length > 0)
{
/*
* If they didn't specify a marker but this is an audio sprite,
* we should never play the entire thing
*/
return this;
}
if (marker !== '')
{
if (this.markers[marker])
{
this.currentMarker = marker;
// Playing a marker? Then we default to the marker values
this.position = this.markers[marker].start;
this.volume = this.markers[marker].volume;
this.loop = this.markers[marker].loop;
this.duration = this.markers[marker].duration;
this.durationMS = this.markers[marker].durationMS;
if (typeof volume !== 'undefined')
{
this.volume = volume;
}
if (typeof loop !== 'undefined')
{
this.loop = loop;
}
this._tempMarker = marker;
this._tempPosition = this.position;
this._tempVolume = this.volume;
this._tempLoop = this.loop;
}
else
{
console.warn('Phaser.Sound.play: audio marker ' + marker + ' doesn\'t exist');
return this;
}
}
else
{
position = position || 0;
if (volume === undefined) { volume = this._volume; }
if (loop === undefined) { loop = this.loop; }
this.position = Math.max(0, position);
this.volume = volume;
this.loop = loop;
this.duration = 0;
this.durationMS = 0;
this._tempMarker = marker;
this._tempPosition = position;
this._tempVolume = volume;
this._tempLoop = loop;
}
if (this.usingWebAudio)
{
// Does the sound need decoding?
if (this.game.cache.isSoundDecoded(this.key))
{
this._createSourceAndConnect();
if (this.loop && marker === '')
{
this._sound.loop = true;
}
if (!this.loop && marker === '')
{
this._addOnEndedHandler();
}
this.totalDuration = this._sound.buffer.duration;
if (this.duration === 0)
{
this.duration = this.totalDuration;
this.durationMS = Math.ceil(this.totalDuration * 1000);
}
if (this.loop && marker === '')
{
this._startSource(0, 0);
}
else
{
this._startSource(0, this.position, this.duration);
}
this.isPlaying = true;
this.paused = false;
this.startTime = this.game.time.time;
this.currentTime = 0;
this.stopTime = this.startTime + this.durationMS;
if (onPlay)
{
this.onPlay.dispatch(this);
}
}
else
{
this.pendingPlayback = true;
if (this.game.cache.getSound(this.key) && this.game.cache.getSound(this.key).isDecoding === false)
{
this.game.sound.decode(this.key, this);
}
}
}
else if (this.game.cache.getSound(this.key) && this.game.cache.getSound(this.key).locked)
{
this.game.cache.reloadSound(this.key);
this.pendingPlayback = true;
}
else if (this._sound && (this.game.device.cocoonJS || this._sound.readyState === 4))
{
this._sound.play();
this._sound.loop = this.loop;
// This doesn't become available until you call play(), wonderful ...
this.totalDuration = this._sound.duration;
if (this.duration === 0)
{
this.duration = this.totalDuration;
this.durationMS = this.totalDuration * 1000;
}
this._globalVolume = this.game.sound.volume;
this._sound.currentTime = this.position;
this._sound.muted = this._muted;
if (this._muted || this.game.sound.mute)
{
this._sound.volume = 0;
}
else
{
this._sound.volume = this._volume * this._globalVolume;
}
this._pendingStart = !this.currentMarker;
this.isPlaying = true;
this.paused = false;
this._tempPause = 0;
this.startTime = this.game.time.time;
this.currentTime = 0;
this.stopTime = this.startTime + this.durationMS;
if (onPlay)
{
this.onPlay.dispatch(this);
}
}
else
{
this.pendingPlayback = true;
}
if (this.playOnce)
{
if (this.loop)
{
console.warn('Phaser.Sound.play: audio clip ' + this.name + ' cannot be deleted while looping.');
}
this._markedToDelete = true;
this._removeFromSoundManager = true;
}
return this;
},
/**
* Restart the sound, or a marked section of it.
*
* @method Phaser.Sound#restart
* @param {string} [marker=''] - If you want to play a marker then give the key here, otherwise leave blank to play the full sound.
* @param {number} [position=0] - The starting position to play the sound from - this is ignored if you provide a marker.
* @param {number} [volume=1] - Volume of the sound you want to play.
* @param {boolean} [loop=false] - Loop when it finished playing?
*/
restart: function (marker, position, volume, loop)
{
marker = marker || '';
position = position || 0;
volume = volume || 1;
if (loop === undefined) { loop = false; }
this.play(marker, position, volume, loop, true);
},
/**
* Pauses the sound.
*
* @method Phaser.Sound#pause
*/
pause: function ()
{
if (this.isPlaying)
{
this.paused = true;
this.pausedPosition = this.currentTime;
this.pausedTime = this.game.time.time;
this._tempPause = this._sound.currentTime;
this.onPause.dispatch(this);
this.stop();
}
},
/**
* Resumes the sound.
*
* @method Phaser.Sound#resume
*/
resume: function ()
{
if (this.paused)
{
if (this.usingWebAudio)
{
var p = Math.max(0, this.position + (this.pausedPosition / 1000));
this._createSourceAndConnect();
if (this.currentMarker === '')
{
if (this.loop)
{
this._sound.loop = true;
}
else
{
this._addOnEndedHandler();
}
}
var duration = this.duration - (this.pausedPosition / 1000);
if (this.loop && this.currentMarker === '')
{
this._startSource(0, p);
}
else
{
this._startSource(0, p, duration);
}
}
else
{
this._pendingStart = !this.currentMarker;
this._sound.currentTime = this._tempPause;
this._tempPause = 0;
this._sound.play();
}
this.isPlaying = true;
this.paused = false;
this.startTime += (this.game.time.time - this.pausedTime);
this.onResume.dispatch(this);
}
},
/**
* Stop playing this sound.
*
* @method Phaser.Sound#stop
*/
stop: function ()
{
if (this.isPlaying && this._sound)
{
if (this.usingWebAudio)
{
this._stopSourceAndDisconnect();
}
else if (this.usingAudioTag)
{
this._sound.pause();
this._sound.currentTime = 0;
}
}
this.pendingPlayback = false;
this.isPlaying = false;
if (!this.paused)
{
var prevMarker = this.currentMarker;
if (this.currentMarker !== '')
{
this.onMarkerComplete.dispatch(this.currentMarker, this);
}
this.currentMarker = '';
if (this.fadeTween !== null)
{
this.fadeTween.stop();
}
this.onStop.dispatch(this, prevMarker);
}
},
/**
* Starts this sound playing (or restarts it if already doing so) and sets the volume to zero.
* Then increases the volume from 0 to 1 over the duration specified.
*
* At the end of the fade Sound.onFadeComplete is dispatched with this Sound object as the first parameter,
* and the final volume (1) as the second parameter.
*
* @method Phaser.Sound#fadeIn
* @param {number} [duration=1000] - The time in milliseconds over which the Sound should fade in.
* @param {boolean} [loop=false] - Should the Sound be set to loop? Note that this doesn't cause the fade to repeat.
* @param {string} [marker=(current marker)] - The marker to start at; defaults to the current (last played) marker. To start playing from the beginning specify specify a marker of `''`.
*/
fadeIn: function (duration, loop, marker)
{
if (loop === undefined) { loop = false; }
if (marker === undefined) { marker = this.currentMarker; }
if (this.paused)
{
return;
}
this.play(marker, 0, 0, loop);
this.fadeTo(duration, 1);
},
/**
* Decreases the volume of this Sound from its current value to 0 over the duration specified.
* At the end of the fade Sound.onFadeComplete is dispatched with this Sound object as the first parameter,
* and the final volume (0) as the second parameter.
*
* @method Phaser.Sound#fadeOut
* @param {number} [duration=1000] - The time in milliseconds over which the Sound should fade out.
*/
fadeOut: function (duration)
{
this.fadeTo(duration, 0);
},
/**
* Fades the volume of this Sound from its current value to the given volume over the duration specified.
* At the end of the fade Sound.onFadeComplete is dispatched with this Sound object as the first parameter,
* and the final volume (volume) as the second parameter.
*
* @method Phaser.Sound#fadeTo
* @param {number} [duration=1000] - The time in milliseconds during which the Sound should fade out.
* @param {number} [volume] - The volume which the Sound should fade to. This is a value between 0 and 1.
*/
fadeTo: function (duration, volume)
{
if (!this.isPlaying || this.paused || volume === this.volume)
{
return;