Kiwi.Sound.Audio Class
The Audio Object contains the functionality for playing a singular sound in a Kiwi Game. The Audio can contain Audio Sprites which is a nice way to play audio on mobile devices. Audio Objects do not 'die' when game states are switched or changed.
Constructor
Kiwi.Sound.Audio
-
game
-
key
-
volume
-
loop
Parameters:
-
game
Kiwi.GameThe game that this piece of audio belongs to.
-
key
StringThe key to which which piece of audio should be loaded from the AudioLibrary.
-
volume
NumberA number between 0 (silence) and 1 (loud).
-
loop
BooleanIf the audio should loop when it is finished or just stop.
Returns:
This object
Item Index
Properties
- _buffer
- _currentMarker
- _currentTime
- _decoded
- _file
- _game
- _loop
- _markers
- _muted
- _muteVolume
- _pending
- _playable
- _sound
- _startTime
- _usingAudioTag
- _usingWebAudio
- _volume
- context
- duration
- gainNode
- id
- isPlaying
- key
- loop
- masterGainNode
- mute
- onComplete
- onLoop
- onMute
- onPause
- onPlay
- onResume
- onStop
- paused
- playable
- ready
- totalDuration
- volume
Methods
_decode
()
private
Decodes the audio data to make it playable. By default the audio should already have been decoded when it was loaded.
_setAudio
()
private
Retrieves the audio data from the file store.
addMarker
-
name
-
start
-
stop
-
[loop=false]
Adds a new marker to the audio which will then allow for that section of audio to be played.
Parameters:
-
name
StringThe name of the marker that you are adding.
-
start
NumberThe starting point of the audio. In seconds.
-
stop
NumberThe stopping point of the audio. In seconds.
-
[loop=false]
Boolean optionalIf the marker's pieve of audio should loop or not.
destroy
()
public
This method handles the destruction of all of the properties when this audio is not longer needed. You call this method when you want this method to be removed on the next garbage collection cycle.
objType
()
String
public
The type of object that this is.
Returns:
"Audio"
pause
()
public
Pauses the sound so that you can resume it from at point to paused it at.
play
-
[marker]
-
[forceRestart=false]
Plays the current sound/audio from the start.
Parameters:
-
[marker]
String optionalThe marker that is to be played. If no marker is specified then it will play the current marker (which would be the whole audio piece if no marker was ever added).
-
[forceRestart=false]
Boolean optionalForce the audio to stop and start again. Otherwise if the audio was already playing it would ignore the play.
removeMarker
-
name
Removes a currently existing marker from this audio.
Parameters:
-
name
Stringname of the audio that you want to remove.
resume
()
public
Plays the sound from when you paused the sound.
stop
()
public
Stop the sound from playing.
update
()
public
The update loop that gets executed every frame.
Properties
_buffer
Any
private
Web Audio API ONLY - The audio buffer that is to be used when playing audio segments.
_currentMarker
String
private
The current marker that is being used.
Default: 'default'
_currentTime
Number
private
When the audio is playing, this is the current time we are at with it playing. In milliseconds
_decoded
Boolean
private
Web Audio API ONLY - A boolean to indicate if the audio has been decoded or not yet. If not you will need to run the decode() method.
Default: false
_file
File
private
The property containing the file information about the audio.
_loop
Boolean
private
A boolean indicating whether this piece of audio should loop or not.
_markers
Object
private
A collection of all of the markers that are on this piece of audio.
_muted
Boolean
private
A private indicator of whether this audio is currently muted or not.
Default: false
_muteVolume
Number
private
A private property that holds the volume before the sound was muted. Used so that when unmuted the sound will resume at its old spot.
_pending
Boolean
private
If the sound needs to be played but we are waiting on particular condition. In the case of CocoonJS, we generally are waiting for the audio 'length' property to have a value.
_playable
Boolean
private
A flag that indicates whether the sound is ready to be played or not. If not then this indicates that we are awaiting a user event.
_sound
Any
private
This is the piece of audio that either method will use to play audio. E.g. In the case of the Web Audio API this is the sound buffer source, in which the audio plays from. In the case of Audio Tags this is the Audio Tag itself.
_startTime
Number
private
When the audio started playing. In milliseconds
_usingAudioTag
Boolean
private
A boolean indicating whether or not that audio tags are being used to generate sounds.
_usingWebAudio
Boolean
private
A boolean indicating whether or not the webAuduio api is being used.
_volume
Number
private
A number between 0 and 1 representing the current volume of this audio piece.
Default: 1
context
Any
public
Web Audio API ONLY - A reference to the audio context that the game's audio manager has.
duration
Number
public
The current duration of the section of audio that is being played. In milliseconds.
gainNode
Any
public
Web Audio API ONLY - This sounds local gainNode that it uses.
id
Number
public
A unique ID that this audio gets assigned by the audio manager it belongs to when it is created.
isPlaying
Boolean
public
Indicates whether or not the sound is currently playing.
Default: false
key
String
public
The key that was used to get the audio from the AudioLibrary.
loop
Boolean
public
READ ONLY: Returns a boolean indicating if the current audio marker playing is/will loop.
masterGainNode
Any
public
Web Audio API ONLY - A reference to the master gain node on the audio manager.
mute
Boolean
public
Mutes the sound and makes it 'silent'. This will not stop the sound from playing, or events from being dispatched due when the sound has finished/is looping.
onComplete
Kiwi.Signal
public
A Kiwi Signal that dispatches a event when the audio completes and does not loop.
onLoop
Kiwi.Signal
public
A Kiwi Signal that dispatches a event when the audio finishes and starts again, due to it looping.
paused
Boolean
public
A indicator of if the sound is currently paused.
Default: false
playable
Boolean
private
Returns whether or not the sound is 'playable' or not. The only time the sound would be not 'playable' is on iOS devices when a mouse/touch event has not fired. Devs should treat this property as READ ONLY.
ready
Boolean
public
A boolean that controls/knows if the audio is ready to be played or not. This is just an indicator of if the file has been retrieved successfully from the file store or not.
totalDuration
Number
public
The total duration of the audio in seconds.
volume
Number
public
Used to control the current volume for this sound. 0 is silent, 1 is full volume.