Class: Phaser.Sound
Constructor
new Sound(game, key, volume, loop)
The Sound class constructor.
Parameters
Name | Type | Argument | Default | Description |
---|---|---|---|---|
game | Phaser.Game | Reference to the current game instance. | ||
key | string | Asset key for the sound. | ||
volume | number | <optional> | 1 | Default value for the volume, between 0 and 1. |
loop | boolean | <optional> | false | Whether or not the sound will loop. |
- Source code: sound/Sound.js (Line 17)
Public Properties
- Source code: sound/Sound.js (Line 144)
- Source code: sound/Sound.js (Line 57)
- Source code: sound/Sound.js (Line 52)
- Source code: sound/Sound.js (Line 121)
- Source code: sound/Sound.js (Line 73)
- Source code: sound/Sound.js (Line 78)
- Source code: sound/Sound.js (Line 83)
- Source code: sound/Sound.js (Line 160)
- Source code: sound/Sound.js (Line 126)
- Source code: sound/Sound.js (Line 170)
- Source code: sound/Sound.js (Line 27)
- Source code: sound/Sound.js (Line 1087)
- Source code: sound/Sound.js (Line 1074)
- Source code: sound/Sound.js (Line 115)
- Source code: sound/Sound.js (Line 37)
- Source code: sound/Sound.js (Line 42)
- Source code: sound/Sound.js (Line 47)
- Source code: sound/Sound.js (Line 165)
- Source code: sound/Sound.js (Line 1100)
- Source code: sound/Sound.js (Line 32)
- Source code: sound/Sound.js (Line 220)
- Source code: sound/Sound.js (Line 260)
- Source code: sound/Sound.js (Line 240)
- Source code: sound/Sound.js (Line 255)
- Source code: sound/Sound.js (Line 250)
- Source code: sound/Sound.js (Line 230)
- Source code: sound/Sound.js (Line 225)
- Source code: sound/Sound.js (Line 235)
- Source code: sound/Sound.js (Line 245)
- Source code: sound/Sound.js (Line 138)
- Source code: sound/Sound.js (Line 99)
- Source code: sound/Sound.js (Line 104)
- Source code: sound/Sound.js (Line 109)
- Source code: sound/Sound.js (Line 132)
- Source code: sound/Sound.js (Line 88)
- Source code: sound/Sound.js (Line 68)
- Source code: sound/Sound.js (Line 93)
- Source code: sound/Sound.js (Line 62)
- Source code: sound/Sound.js (Line 155)
- Source code: sound/Sound.js (Line 150)
- Source code: sound/Sound.js (Line 1155)
allowMultiple : boolean
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.
autoplay : boolean
Boolean indicating whether the sound should start automatically.
context :AudioContext
Reference to the AudioContext instance.
currentMarker : string
The string ID of the currently playing marker, if any.
currentTime : number
The current time the sound is at.
duration : number
The duration of the current sound marker in seconds.
durationMS : number
The duration of the current sound marker in ms.
externalNode : Object
If defined this Sound won't connect to the SoundManager master gain node, but will instead connect to externalNode.
fadeTween : Phaser.Tween
The tween that fades the audio, set via Sound.fadeIn and Sound.fadeOut.
gainNode : Object
The gain node in a Web Audio system.
game : Phaser.Game
A reference to the currently running Game.
[readonly] isDecoded : boolean
Returns true if the sound file has decoded.
[readonly] isDecoding : boolean
Returns true if the sound file is still decoding.
isPlaying : boolean
true if the sound is currently playing, otherwise false.
key : string
Asset key for the sound.
loop : boolean
Whether or not the sound or current sound marker will loop.
markers : Object
The sound markers.
masterGainNode : Object
The master gain node in a Web Audio system.
mute : boolean
Gets or sets the muted state of this sound.
name : string
Name of the sound.
onDecoded : Phaser.Signal
The onDecoded event is dispatched when the sound has finished decoding (typically for mp3 files)
onFadeComplete : Phaser.Signal
The onFadeComplete event is dispatched when this sound finishes fading either in or out.
onLoop : Phaser.Signal
The onLoop event is dispatched when this sound loops during playback.
onMarkerComplete : Phaser.Signal
The onMarkerComplete event is dispatched when a marker within this sound completes playback.
onMute : Phaser.Signal
The onMute event is dispatched when this sound is muted.
onPause : Phaser.Signal
The onPause event is dispatched when this sound is paused.
onPlay : Phaser.Signal
The onPlay event is dispatched each time this sound is played.
onResume : Phaser.Signal
The onResume event is dispatched when this sound is resumed from a paused state.
onStop : Phaser.Signal
The onStop event is dispatched when this sound stops playback.
override : boolean
if true when you play this sound it will always start from the beginning.
paused : boolean
true if the sound is paused, otherwise false.
pausedPosition : number
The position the sound had reached when it was paused.
pausedTime : number
The game time at which the sound was paused.
[readonly] pendingPlayback : boolean
true if the sound file is pending playback
position : number
The position of the current sound marker.
startTime : number
The time the Sound starts at (typically 0 unless starting from a marker)
stopTime : number
The time the sound stopped.
totalDuration : number
The total duration of the sound in seconds.
usingAudioTag : boolean
true if the sound is being played via the Audio tag.
[readonly] usingWebAudio : boolean
true if this sound is being played with Web Audio.
volume : number
Gets or sets the volume of this sound, a value between 0 and 1. The value given is clamped to the range 0 to 1.
Public Methods
- Source code: sound/Sound.js (Line 348)
- Source code: sound/Sound.js (Line 1035)
- Source code: sound/Sound.js (Line 924)
- Source code: sound/Sound.js (Line 952)
- Source code: sound/Sound.js (Line 966)
- Source code: sound/Sound.js (Line 492)
- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source code: sound/Sound.js (Line 388)
- Source code: sound/Sound.js (Line 763)
- Source code: sound/Sound.js (Line 505)
- Source code: sound/Sound.js (Line 377)
- Source code: sound/Sound.js (Line 743)
- Source code: sound/Sound.js (Line 782)
- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source code: sound/Sound.js (Line 332)
- Source code: sound/Sound.js (Line 858)
- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source code: sound/Sound.js (Line 404)
- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source code: sound/Sound.js (Line 1013)
addMarker(name, start, duration, volume, loop)
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.
Parameters
Name | Type | Argument | Default | Description |
---|---|---|---|---|
name | string | A unique name for this marker, i.e. 'explosion', 'gunshot', etc. | ||
start | number | The start point of this marker in the audio file, given in seconds. 2.5 = 2500ms, 0.5 = 500ms, etc. | ||
duration | number | <optional> | 1 | The duration of the marker in seconds. 2.5 = 2500ms, 0.5 = 500ms, etc. |
volume | number | <optional> | 1 | The volume the sound will play back at, between 0 (silent) and 1 (full volume). |
loop | boolean | <optional> | false | Sets if the sound will loop or not. |
destroy(remove)
Destroys this sound and all associated events and removes it from the SoundManager.
Parameters
Name | Type | Argument | Default | Description |
---|---|---|---|---|
remove | boolean | <optional> | true | If true this Sound is automatically removed from the SoundManager. |
fadeIn(duration, loop, marker)
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.
Parameters
Name | Type | Argument | Default | Description |
---|---|---|---|---|
duration | number | <optional> | 1000 | The time in milliseconds over which the Sound should fade in. |
loop | boolean | <optional> | false | Should the Sound be set to loop? Note that this doesn't cause the fade to repeat. |
marker | string | <optional> | (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 |
fadeOut(duration)
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.
Parameters
Name | Type | Argument | Default | Description |
---|---|---|---|---|
duration | number | <optional> | 1000 | The time in milliseconds over which the Sound should fade out. |
fadeTo(duration, volume)
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.
Parameters
Name | Type | Argument | Default | Description |
---|---|---|---|---|
duration | number | <optional> | 1000 | The time in milliseconds during which the Sound should fade out. |
volume | number | <optional> | The volume which the Sound should fade to. This is a value between 0 and 1. |
loopFull(volume) → {Phaser.Sound}
Loops this entire sound. If you need to loop a section of it then use Sound.play and the marker and loop parameters.
Parameters
Name | Type | Argument | Default | Description |
---|---|---|---|---|
volume | number | <optional> | 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). |
Returns
This sound instance.
<internal> onEndedHandler()
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.
pause()
Pauses the sound.
play(marker, position, volume, loop, forceRestart) → {Phaser.Sound}
Play this sound, or a marked section of it.
Parameters
Name | Type | Argument | Default | Description |
---|---|---|---|---|
marker | string | <optional> | '' | If you want to play a marker then give the key here, otherwise leave blank to play the full sound. |
position | number | <optional> | 0 | The starting position to play the sound from - this is ignored if you provide a marker. |
volume | number | <optional> | 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). |
loop | boolean | <optional> | 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. |
forceRestart | boolean | <optional> | true | If the sound is already playing you can set forceRestart to restart it from the beginning. |
Returns
This sound instance.
removeMarker(name)
Removes a marker from the sound.
Parameters
Name | Type | Description |
---|---|---|
name | string | The key of the marker to remove. |
restart(marker, position, volume, loop)
Restart the sound, or a marked section of it.
Parameters
Name | Type | Argument | Default | Description |
---|---|---|---|---|
marker | string | <optional> | '' | If you want to play a marker then give the key here, otherwise leave blank to play the full sound. |
position | number | <optional> | 0 | The starting position to play the sound from - this is ignored if you provide a marker. |
volume | number | <optional> | 1 | Volume of the sound you want to play. |
loop | boolean | <optional> | false | Loop when it finished playing? |
resume()
Resumes the sound.
<internal> soundHasUnlocked(key)
Called automatically when this sound is unlocked.
Parameters
Name | Type | Description |
---|---|---|
key | string | The Phaser.Cache key of the sound file to check for decoding. |
stop()
Stop playing this sound.
<internal> update()
Called automatically by Phaser.SoundManager.
<internal> updateGlobalVolume(globalVolume)
Called automatically by SoundManager.volume.
Sets the volume of AudioTag Sounds as a percentage of the Global Volume.
You should not normally call this directly.
Parameters
Name | Type | Description |
---|---|---|
globalVolume | float | The global SoundManager volume. |
© 2016 Richard Davey, Photon Storm Ltd.
Licensed under the MIT License.
http://phaser.io/docs/2.6.2/Phaser.Sound.html