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:
Members
-
autoplay
-
- Source:
Properties:
Name Type Description autoplayboolean Boolean indicating whether the sound should start automatically.
-
context
-
- Source:
Properties:
Name Type Description contextAudioContext Reference to the AudioContext instance.
-
currentMarker
-
- Source:
Properties:
Name Type Description currentMarkerstring The string ID of the currently playing marker, if any.
-
currentTime
-
- Source:
Properties:
Name Type Description currentTimenumber The current time the sound is at.
-
duration
-
- Source:
Properties:
Name Type Description durationnumber The duration of the current sound marker in seconds.
-
durationMS
-
- Source:
Properties:
Name Type Description durationMSnumber The duration of the current sound marker in ms.
-
externalNode
-
- Source:
Properties:
Name Type Description externalNodeobject If defined this Sound won't connect to the SoundManager master gain node, but will instead connect to externalNode.
-
gainNode
-
- Source:
Properties:
Name Type Description gainNodeobject The gain node in a Web Audio system.
-
game
-
A reference to the currently running Game.
- Source:
Properties:
Name Type Description gamePhaser.Game -
<readonly> isDecoded
-
- Source:
Properties:
Name Type Description isDecodedboolean Returns true if the sound file has decoded.
-
<readonly> isDecoding
-
- Source:
Properties:
Name Type Description isDecodingboolean Returns true if the sound file is still decoding.
-
isPlaying
-
- Default Value:
- false
- Source:
Properties:
Name Type Description isPlayingboolean true if the sound is currently playing, otherwise false.
-
key
-
- Source:
Properties:
Name Type Description keystring Asset key for the sound.
-
loop
-
- Source:
Properties:
Name Type Description loopboolean Whether or not the sound or current sound marker will loop.
-
markers
-
- Source:
Properties:
Name Type Description markersobject The sound markers.
-
masterGainNode
-
- Source:
Properties:
Name Type Description masterGainNodeobject The master gain node in a Web Audio system.
-
mute
-
- Source:
Properties:
Name Type Description muteboolean Gets or sets the muted state of this sound.
-
name
-
- Source:
Properties:
Name Type Description namestring Name of the sound.
-
onDecoded
-
- Source:
Properties:
Name Type Description onDecodedPhaser.Signal The onDecoded event is dispatched when the sound has finished decoding (typically for mp3 files)
-
onLoop
-
- Source:
Properties:
Name Type Description onLoopPhaser.Signal The onLoop event is dispatched when this sound loops during playback.
-
onMarkerComplete
-
- Source:
Properties:
Name Type Description onMarkerCompletePhaser.Signal The onMarkerComplete event is dispatched when a marker within this sound completes playback.
-
onMute
-
- Source:
Properties:
Name Type Description onMutePhaser.Signal The onMouse event is dispatched when this sound is muted.
-
onPause
-
- Source:
Properties:
Name Type Description onPausePhaser.Signal The onPause event is dispatched when this sound is paused.
-
onPlay
-
- Source:
Properties:
Name Type Description onPlayPhaser.Signal The onPlay event is dispatched each time this sound is played.
-
onResume
-
- Source:
Properties:
Name Type Description onResumePhaser.Signal The onResume event is dispatched when this sound is resumed from a paused state.
-
onStop
-
- Source:
Properties:
Name Type Description onStopPhaser.Signal The onStop event is dispatched when this sound stops playback.
-
override
-
- Default Value:
- false
- Source:
Properties:
Name Type Description overrideboolean if true when you play this sound it will always start from the beginning.
-
paused
-
- Default Value:
- false
- Source:
Properties:
Name Type Description pausedboolean true if the sound is paused, otherwise false.
-
pausedPosition
-
- Source:
Properties:
Name Type Description pausedPositionnumber The position the sound had reached when it was paused.
-
pausedTime
-
- Source:
Properties:
Name Type Description pausedTimenumber The game time at which the sound was paused.
-
<readonly> pendingPlayback
-
- Source:
Properties:
Name Type Description pendingPlaybackboolean true if the sound file is pending playback
-
position
-
- Source:
Properties:
Name Type Description positionnumber The position of the current sound marker.
-
startTime
-
- Default Value:
- 0
- Source:
Properties:
Name Type Description startTimenumber The time the Sound starts at (typically 0 unless starting from a marker)
-
stopTime
-
- Source:
Properties:
Name Type Description stopTimenumber The time the sound stopped.
-
totalDuration
-
- Source:
Properties:
Name Type Description totalDurationnumber The total duration of the sound in seconds.
-
usingAudioTag
-
- Source:
Properties:
Name Type Description usingAudioTagboolean true if the sound is being played via the Audio tag.
-
<readonly> usingWebAudio
-
- Source:
Properties:
Name Type Description usingWebAudioboolean true if this sound is being played with Web Audio.
-
<readonly> volume
-
- Source:
Properties:
Name Type Description volumenumber Gets or sets the volume of this sound, a value between 0 and 1.
-
volume
-
- Source:
Properties:
Name Type Description volumenumber The sound or sound marker volume. A value between 0 (silence) and 1 (full volume).
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 namestring A unique name for this marker, i.e. 'explosion', 'gunshot', etc.
startnumber The start point of this marker in the audio file, given in seconds. 2.5 = 2500ms, 0.5 = 500ms, etc.
durationnumber The duration of the marker in seconds. 2.5 = 2500ms, 0.5 = 500ms, etc.
volumenumber <optional>
1 The volume the sound will play back at, between 0 (silent) and 1 (full volume).
loopboolean <optional>
false Sets if the sound will loop or not.
- Source:
-
destroy(remove)
-
Destroys this sound and all associated events and removes it from the SoundManager.
Parameters:
Name Type Argument Default Description removeboolean <optional>
true If true this Sound is automatically removed from the SoundManager.
- Source:
-
pause()
-
Pauses the sound.
- Source:
-
play(marker, position, volume, loop, forceRestart) → {Phaser.Sound}
-
Play this sound, or a marked section of it.
Parameters:
Name Type Argument Default Description markerstring <optional>
'' If you want to play a marker then give the key here, otherwise leave blank to play the full sound.
positionnumber <optional>
0 The starting position to play the sound from - this is ignored if you provide a marker.
volumenumber <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).
loopboolean <optional>
false Loop when it finished playing?
forceRestartboolean <optional>
true If the sound is already playing you can set forceRestart to restart it from the beginning.
- Source:
Returns:
This sound instance.
- Type
- Phaser.Sound
-
removeMarker(name)
-
Removes a marker from the sound.
Parameters:
Name Type Description namestring The key of the marker to remove.
- Source:
-
restart(marker, position, volume, loop)
-
Restart the sound, or a marked section of it.
Parameters:
Name Type Argument Default Description markerstring <optional>
'' If you want to play a marker then give the key here, otherwise leave blank to play the full sound.
positionnumber <optional>
0 The starting position to play the sound from - this is ignored if you provide a marker.
volumenumber <optional>
1 Volume of the sound you want to play.
loopboolean <optional>
false Loop when it finished playing?
- Source:
-
resume()
-
Resumes the sound.
- Source:
-
<protected> soundHasUnlocked(key)
-
Called automatically when this sound is unlocked.
Parameters:
Name Type Description keystring The Phaser.Cache key of the sound file to check for decoding.
- Source:
-
stop()
-
Stop playing this sound.
- Source:
-
<protected> update()
-
Called automatically by Phaser.SoundManager.
- Source: