AudioPlayer Class
Grapefruit Audio API, provides an easy interface to use HTML5 Audio The GF Audio API was based on Howler.js
Constructor
AudioPlayer
-
manager
-
audio
-
[settings]
Parameters:
-
manager
AudioManagerAudioManager instance for this audio player
-
audio
ObjectThe preloaded audio file object
-
data
ArrayBuffer | AudioThe actual audio data
-
webAudio
BooleanWhether the file is using webAudio or not
-
decoded
BooleanWhether the data has been decoded yet or not
-
-
[settings]
Object optionalAll the settings for this player instance
-
[autoplay=false]
Boolean optionalWhether to automatically start playing the audio file
-
[loop=false]
Boolean optionalWhether the audio should loop or not
-
[pos3d]
Arrayoptional The 3d position of the audio to play in the form [x, y, z]
-
[sprite]
Object optionalThe audio sprite, if this audio clip has multiple sounds in it. This object is in the form
{ 'sound': [start, duration] }
, and you can use them with.play('sound')
.
-
Item Index
Methods
Properties
Methods
_activeNode
()
AudioPlayer
private
Get the first active audio node.
Returns:
Audio node.
_clearEndTimer
-
timerId
Clear 'onend' timeout before it ends.
Parameters:
-
timerId
NumbertimerId The ID of the sound to be cancelled.
_doFadeStep
()
private
Performs a step in the fade transition
_drainPool
()
private
If there are more than 5 inactive audio nodes in the pool, clear out the rest.
_inactiveNode
-
cb
Get the first inactive audio node. If there is none, create a new one and add it to the pool.
Parameters:
-
cb
Functioncallback Function to call when the audio node is ready.
_load
()
AudioPlayer
private
Load the audio file for this player, this is called from the ctor there is no reason to call it manually.
Returns:
_setupAudioNode
()
Object
private
Setup the gain node and panner for a Web Audio instance.
Returns:
The new audio node.
emit
-
type
-
data
Emits an event which will run all registered listeners for the event type
Parameters:
-
type
StringThe event name to emit
-
data
MixedAny data you want passed along with the event
Returns:
Returns itself.
fade
-
from
-
to
-
len
-
[id]
-
[callback]
Fade a currently playing sound between two volumes.
Parameters:
-
from
NumberThe volume to fade from (0.0 to 1.0).
-
to
NumberThe volume to fade to (0.0 to 1.0).
-
len
NumberTime in milliseconds to fade.
-
[id]
String optionalThe play instance ID.
-
[callback]
Function optionalFired when the fade is complete.
Returns:
Returns itself.
getPosition
-
[id]
Get the position of playback.
Parameters:
-
[id]
String optionalThe play instance ID.
Returns:
getVolume
()
Number
Returns the current volume of the player
Returns:
The current volume
loadSound
-
buffer
Finishes loading the Web Audio API sound and fires the loaded event
Parameters:
-
buffer
ObjectThe decoded buffer sound source.
mute
-
[id]
Mute this sound.
Parameters:
-
[id]
String optionalThe play instance ID.
Returns:
Returns itself.
off
-
type
-
listener
Removes a listener function for an event type
Parameters:
-
type
StringThe event name to emit
-
listener
FunctionThe function to remove
Returns:
Returns itself.
on
-
type
-
listener
Registers a listener function to be run on an event occurance
Parameters:
-
type
StringThe event name to listen for
-
listener
FunctionThe function to execute when the event happens
Returns:
Returns itself.
once
-
type
-
listener
Registers a one-time callback for an event
Parameters:
-
type
StringThe event name to listen for
-
listener
Functionthe callback to call when the event occurs
Returns:
Returns itself.
pause
-
[id]
-
[timerId]
Pause playback and save the current position.
Parameters:
-
[id]
String optionalThe play instance ID.
-
[timerId]
String optionalClear the correct timeout ID.
Returns:
Returns itself.
play
-
[sprite]
-
[callback]
Play a sound from the current time (0 by default).
Parameters:
-
[sprite]
String optionalPlays from the specified position in the sound sprite definition.
-
[callback]
Function optionalReturns the unique playback id for this sound instance.
Returns:
Returns itself.
refreshBuffer
-
loop
-
[id]
Load the sound back into the buffer source.
Parameters:
-
loop
ArrayLoop boolean, pos, and duration.
-
[id]
String optionalThe play instance ID.
seek
-
pos
-
[id]
Set the position of playback.
Parameters:
-
pos
NumberThe position to move current playback to.
-
[id]
String optionalThe play instance ID.
Returns:
Returns itself.
setMuted
-
muted
-
[id]
Set the muted state of this sound.
Parameters:
-
muted
Boolean -
[id]
String optionalThe play instance ID.
Returns:
Returns itself.
setPosition
-
x
-
y
-
z
-
[id]
Set the 3D position of the audio source. The most common usage is to set the 'x' position to affect the left/right ear panning. Setting any value higher than 1.0 will begin to decrease the volume of the sound as it moves further away. NOTE: This only works with Web Audio API, HTML5 Audio playback will not be affected.
Parameters:
-
x
NumberThe x-position of the playback from -1000.0 to 1000.0
-
y
NumberThe y-position of the playback from -1000.0 to 1000.0
-
z
NumberThe z-position of the playback from -1000.0 to 1000.0
-
[id]
String optionalThe play instance ID.
Returns:
Returns itself.
setVolume
-
vol
-
[id]
Sets the current volume of the player
Parameters:
-
vol
NumberThe current volume
-
[id]
String optionalThe play instance ID.
Returns:
Returns itself.
stop
-
[id]
-
[timerId]
Stop playback and reset to start.
Parameters:
-
[id]
String optionalThe play instance ID.
-
[timerId]
String optionalClear the correct timeout ID.
Returns:
Returns itself.
unmute
-
[id]
Unmute this sound.
Parameters:
-
[id]
String optionalThe play instance ID.
Returns:
Returns itself.
Properties
_duration
Number
private
The full duration of the file to play
_file
Object
private
The preloaded audio file object
_loaded
Boolean
private
Has this player data been loaded?
_nodes
Array
private
The actual player nodes, these are either WebAudio Nodes or HTML5 Audio elements.
_onendTimer
Array
private
Array of timeouts to track end events
_volume
Number
private
The current volume of the player
_webAudio
Boolean
private
Does the browser support WebAudio API
autoplay
Boolean
Play the audio immediately after loading
Default: false
format
String
Override the format determined from the extension with this extension
Default: null
key
String
The cache key that uniquely identifies this piece of audio
loop
Boolean
Replay the audio immediately after finishing
Default: false
pos3d
ArrayA 3D position where the audio should sound like it is coming from
Default: [0, 0, -0.5]
sprite
Object
A sound sprite that maps string keys to [start, duration] arrays. These can be used to put multiple sound bits in one file, and play them separately
Default: {}
src
String
The source of the audio, the actual URL to load up
volume
Number
The volume of the audio player
Default: 1
Events
end
Fired when audio finishes playing
Event Payload:
-
id
StringThe id of the node that has finished playing
paused
Fired when audio is paused
Event Payload:
-
id
StringThe id of the node that is paused
play
Fired when audio starts playing
Event Payload:
-
id
StringThe id of the node that is used to play the audio
ready
Fired when the player is ready to play
Event Payload:
-
source
StringThe source URL that will be used as the audio source