# Class: Sound

## Phaser. Sound

The Sound class

#### 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 18

### Members

#### autoplay

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `autoplay` | boolean | Boolean indicating whether the sound should start automatically.

  Source:
    - sound/Sound.js, line 63

#### context

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `context` | AudioContext | Reference to the AudioContext instance.

  Source:
    - sound/Sound.js, line 58

#### currentMarker

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `currentMarker` | string | The string ID of the currently playing marker, if any.

  Source:
    - sound/Sound.js, line 127

#### currentTime

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `currentTime` | number | The current time the sound is at.

  Source:
    - sound/Sound.js, line 79

#### duration

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `duration` | number | The duration of the current sound marker in seconds.

  Source:
    - sound/Sound.js, line 84

#### durationMS

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `durationMS` | number | The duration of the current sound marker in ms.

  Source:
    - sound/Sound.js, line 89

#### externalNode

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `externalNode` | object | If defined this Sound won't connect to the SoundManager master gain node, but will instead connect to externalNode.input.

  Source:
    - sound/Sound.js, line 155

#### gainNode

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `gainNode` | object | The gain node in a Web Audio system.

  Source:
    - sound/Sound.js, line 165

#### game

  A reference to the currently running Game.

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `game` | Phaser.Game |

  Source:
    - sound/Sound.js, line 28

#### <readonly> isDecoded

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `isDecoded` | boolean | Returns true if the sound file has decoded.

  Source:
    - sound/Sound.js, line 818

#### <readonly> isDecoding

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `isDecoding` | boolean | Returns true if the sound file is still decoding.

  Source:
    - sound/Sound.js, line 805

#### isPlaying

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `isPlaying` | boolean | true if the sound is currently playing, otherwise false.

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

#### key

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `key` | string | Asset key for the sound.

  Source:
    - sound/Sound.js, line 38

#### loop

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `loop` | boolean | Whether or not the sound or current sound marker will loop.

  Source:
    - sound/Sound.js, line 43

#### markers

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `markers` | object | The sound markers.

  Source:
    - sound/Sound.js, line 53

#### masterGainNode

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `masterGainNode` | object | The master gain node in a Web Audio system.

  Source:
    - sound/Sound.js, line 160

#### mute

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `mute` | boolean | Gets or sets the muted state of this sound.

  Source:
    - sound/Sound.js, line 831

#### name

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `name` | string | Name of the sound.

  Source:
    - sound/Sound.js, line 33

#### onDecoded

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `onDecoded` | Phaser.Signal | The onDecoded event is dispatched when the sound has finished decoding (typically for mp3 files)

  Source:
    - sound/Sound.js, line 209

#### onLoop

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `onLoop` | Phaser.Signal | The onLoop event is dispatched when this sound loops during playback.

  Source:
    - sound/Sound.js, line 229

#### onMarkerComplete

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `onMarkerComplete` | Phaser.Signal | The onMarkerComplete event is dispatched when a marker within this sound completes playback.

  Source:
    - sound/Sound.js, line 244

#### onMute

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `onMute` | Phaser.Signal | The onMouse event is dispatched when this sound is muted.

  Source:
    - sound/Sound.js, line 239

#### onPause

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `onPause` | Phaser.Signal | The onPause event is dispatched when this sound is paused.

  Source:
    - sound/Sound.js, line 219

#### onPlay

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `onPlay` | Phaser.Signal | The onPlay event is dispatched each time this sound is played.

  Source:
    - sound/Sound.js, line 214

#### onResume

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `onResume` | Phaser.Signal | The onResume event is dispatched when this sound is resumed from a paused state.

  Source:
    - sound/Sound.js, line 224

#### onStop

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `onStop` | Phaser.Signal | The onStop event is dispatched when this sound stops playback.

  Source:
    - sound/Sound.js, line 234

#### override

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `override` | boolean | if true when you play this sound it will always start from the beginning.

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

#### paused

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `paused` | boolean | true if the sound is paused, otherwise false.

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

#### pausedPosition

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `pausedPosition` | number | The position the sound had reached when it was paused.

  Source:
    - sound/Sound.js, line 110

#### pausedTime

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `pausedTime` | number | The game time at which the sound was paused.

  Source:
    - sound/Sound.js, line 115

#### <readonly> pendingPlayback

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `pendingPlayback` | boolean | true if the sound file is pending playback

  Source:
    - sound/Sound.js, line 133

#### position

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `position` | number | The position of the current sound marker.

  Source:
    - sound/Sound.js, line 94

#### startTime

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `startTime` | number | The time the Sound starts at (typically 0 unless starting from a marker)

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

#### stopTime

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `stopTime` | number | The time the sound stopped.

  Source:
    - sound/Sound.js, line 99

#### totalDuration

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `totalDuration` | number | The total duration of the sound in seconds.

  Source:
    - sound/Sound.js, line 68

#### usingAudioTag

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `usingAudioTag` | boolean | true if the sound is being played via the Audio tag.

  Source:
    - sound/Sound.js, line 150

#### <readonly> usingWebAudio

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `usingWebAudio` | boolean | true if this sound is being played with Web Audio.

  Source:
    - sound/Sound.js, line 145

#### <readonly> volume

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `volume` | number | Gets or sets the volume of this sound, a value between 0 and 1.

  Source:
    - sound/Sound.js, line 882

#### volume

  ##### Properties:

  Name | Type | Description
  --- | --- | ---
  `volume` | number | The sound or sound marker volume. A value between 0 (silence) and 1 (full volume).

  Source:
    - sound/Sound.js, line 48

### 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 315

#### 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 769

#### pause()

  Pauses the sound.

  Source:
    - sound/Sound.js, line 651

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

  Source:
    - sound/Sound.js, line 417

  ##### Returns:

  This sound instance.

  Type
    Phaser.Sound

#### 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 343

#### 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 631

#### resume()

  Resumes the sound.

  Source:
    - sound/Sound.js, line 670

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

  Source:
    - sound/Sound.js, line 298

#### stop()

  Stop playing this sound.

  Source:
    - sound/Sound.js, line 723

#### <protected> update()

  Called automatically by Phaser.SoundManager.

  Source:
    - sound/Sound.js, line 354