Phaser. Sound

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 - sound/Sound.js, line 17

Members

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.

Default Value:
  • false
Source - sound/Sound.js, line 144

autoplay :boolean

Boolean indicating whether the sound should start automatically.

Source - sound/Sound.js, line 62

context :AudioContext

Reference to the AudioContext instance.

Source - sound/Sound.js, line 57

currentMarker :string

The string ID of the currently playing marker, if any.

Source - sound/Sound.js, line 126

currentTime :number

The current time the sound is at.

Source - sound/Sound.js, line 78

duration :number

The duration of the current sound marker in seconds.

Source - sound/Sound.js, line 83

durationMS :number

The duration of the current sound marker in ms.

Source - sound/Sound.js, line 88

externalNode :object

If defined this Sound won't connect to the SoundManager master gain node, but will instead connect to externalNode.

Source - sound/Sound.js, line 160

gainNode :object

The gain node in a Web Audio system.

Source - sound/Sound.js, line 170

game :Phaser.Game

A reference to the currently running Game.

Source - sound/Sound.js, line 27

<readonly> isDecoded :boolean

Returns true if the sound file has decoded.

Source - sound/Sound.js, line 938

<readonly> isDecoding :boolean

Returns true if the sound file is still decoding.

Source - sound/Sound.js, line 925

isPlaying :boolean

true if the sound is currently playing, otherwise false.

Default Value:
  • false
Source - sound/Sound.js, line 120

key :string

Asset key for the sound.

Source - sound/Sound.js, line 37

loop :boolean

Whether or not the sound or current sound marker will loop.

Source - sound/Sound.js, line 42

markers :object

The sound markers.

Source - sound/Sound.js, line 52

masterGainNode :object

The master gain node in a Web Audio system.

Source - sound/Sound.js, line 165

mute :boolean

Gets or sets the muted state of this sound.

Source - sound/Sound.js, line 951

name :string

Name of the sound.

Source - sound/Sound.js, line 32

onDecoded :Phaser.Signal

The onDecoded event is dispatched when the sound has finished decoding (typically for mp3 files)

Source - sound/Sound.js, line 214

onFadeComplete :Phaser.Signal

The onFadeComplete event is dispatched when this sound finishes fading either in or out.

Source - sound/Sound.js, line 254

onLoop :Phaser.Signal

The onLoop event is dispatched when this sound loops during playback.

Source - sound/Sound.js, line 234

onMarkerComplete :Phaser.Signal

The onMarkerComplete event is dispatched when a marker within this sound completes playback.

Source - sound/Sound.js, line 249

onMute :Phaser.Signal

The onMouse event is dispatched when this sound is muted.

Source - sound/Sound.js, line 244

onPause :Phaser.Signal

The onPause event is dispatched when this sound is paused.

Source - sound/Sound.js, line 224

onPlay :Phaser.Signal

The onPlay event is dispatched each time this sound is played.

Source - sound/Sound.js, line 219

onResume :Phaser.Signal

The onResume event is dispatched when this sound is resumed from a paused state.

Source - sound/Sound.js, line 229

onStop :Phaser.Signal

The onStop event is dispatched when this sound stops playback.

Source - sound/Sound.js, line 239

override :boolean

if true when you play this sound it will always start from the beginning.

Default Value:
  • false
Source - sound/Sound.js, line 138

paused :boolean

true if the sound is paused, otherwise false.

Default Value:
  • false
Source - sound/Sound.js, line 104

pausedPosition :number

The position the sound had reached when it was paused.

Source - sound/Sound.js, line 109

pausedTime :number

The game time at which the sound was paused.

Source - sound/Sound.js, line 114

<readonly> pendingPlayback :boolean

true if the sound file is pending playback

Source - sound/Sound.js, line 132

position :number

The position of the current sound marker.

Source - sound/Sound.js, line 93

startTime :number

The time the Sound starts at (typically 0 unless starting from a marker)

Default Value:
  • 0
Source - sound/Sound.js, line 73

stopTime :number

The time the sound stopped.

Source - sound/Sound.js, line 98

totalDuration :number

The total duration of the sound in seconds.

Source - sound/Sound.js, line 67

usingAudioTag :boolean

true if the sound is being played via the Audio tag.

Source - sound/Sound.js, line 155

<readonly> usingWebAudio :boolean

true if this sound is being played with Web Audio.

Source - sound/Sound.js, line 150

<readonly> volume :number

Gets or sets the volume of this sound, a value between 0 and 1.

Source - sound/Sound.js, line 1002

volume :number

The sound or sound marker volume. A value between 0 (silence) and 1 (full volume).

Source - sound/Sound.js, line 47

Methods

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

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.

Source - sound/Sound.js, line 335

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.

Source - sound/Sound.js, line 886

fadeIn(duration, loop)

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.

Source - sound/Sound.js, line 800

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.

Source - sound/Sound.js, line 825

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.

Source - sound/Sound.js, line 839

pause()

Pauses the sound.

Source - sound/Sound.js, line 679

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 it finished playing?

forceRestart boolean <optional>
true

If the sound is already playing you can set forceRestart to restart it from the beginning.

Returns:

This sound instance.

Source - sound/Sound.js, line 438

removeMarker(name)

Removes a marker from the sound.

Parameters:
Name Type Description
name string

The key of the marker to remove.

Source - sound/Sound.js, line 363

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?

Source - sound/Sound.js, line 659

resume()

Resumes the sound.

Source - sound/Sound.js, line 697

<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.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source - sound/Sound.js, line 319

stop()

Stop playing this sound.

Source - sound/Sound.js, line 750

<internal> update()

Called automatically by Phaser.SoundManager.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source - sound/Sound.js, line 374
Phaser Copyright © 2012-2014 Photon Storm Ltd.
Documentation generated by JSDoc 3.3.0-dev on Tue Jan 06 2015 06:32:19 GMT-0000 (GMT) using the DocStrap template.