Sound Plugin¶
Contents
Sound Setup¶
In order to enable sound in a game, the first thing to do is include the frigame.sound.js file.
Changed in version 2.2.0: The frigame.sound.js plugin depends on frigame.domready.js, so make sure to include it first.
As not every browser supports sound, and not every browser supports the same audio formats, there are a few considerations to make:
In order to have the best browser support, it is better to encode the same audio file twice, resulting in one mp3 file and one ogg/vorbis file
For old browsers that do not support the HTML5 Audio element, a flash fallback can be provided through soundManager2
As pointed out earlier, friGame supports the soundManager2 module, but it is entirely optional. In order to use it, include the soundmanager2.js file, and place the soundmanager2.swf file (Flash 8 version, no debug) in the same directory as the HTML file.
The audio formats supported by friGame are the following:
Uncompressed PCM/WAVE (default file extension .wav) (Not recommended as it results in huge files)
Ogg/Vorbis (default file extension .ogg or .oga)
MP3 (default file extension .mp3) (Either natively with the HTML5 Audio element, or through soundManager2)
Changed in version 2.2.0: New formats supported:
Ogg/Opus (default file extension .opus)
mp4/aac (default file extension .aac or .m4a or .mp4)
Sound API¶
-
friGame.resourceManager.
addSound
(name, soundURLs[, options])¶ As with animations and gradients, sounds are created through the resourceManager.
Once all the resources have been loaded, the newly created sound can be accessed with:
friGame.resources[name]
Changed in version 2.1.0: Added the streaming option
- Arguments
name (string) – The name of the sound
soundURLs – The URL of the sound files
options – An object literal containing the sound options
- Returns
The resource manager object
The soundURLs parameter can have 3 forms:
A string containing the URL of the sound file
An array containing the URLs of the sound file in different formats
An object literal having as key the sound file format and as value the sound file
Note
For the first 2 forms of the soundURLs parameter the file type is determined by its extension. If the file extension is different from the default, the third form of the soundURLs parameter must be used.
Options may include:
- volume
the sound volume in folating point from 0 (quiet) to 1 (loud – default)
- muted
true if the sound is muted else false (default)
- streaming
true to prefer using HTML5 Audio over the Web Audio API, else false (default)
Example:
friGame.resourceManager.addSound('mySound', './mySound.mp3', { volume: 0.8, muted: false }); friGame.resourceManager.addSound('mySound', ['./mySound.ogg', './mySound.mp3']); friGame.resourceManager.addSound('mySound', { ogg: './mySound.different_extension', mp3: './mySound.another_extension' });
-
sound.
setVolume
([options])¶ This function changes the volume of the sound object.
- Arguments
options – An object literal
- Returns
The sound object
Options may include:
- volume
the sound volume in folating point from 0 (quiet) to 1 (loud – default)
- muted
true if the sound is muted else false (default)
Example:
friGame.resources.mySound.setVolume({ volume: 0.5 });
-
sound.
play
([options])¶ This function plays the sound.
- Arguments
options – An object literal
- Returns
The sound object
Options may include:
- volume
the sound volume in folating point from 0 (quiet) to 1 (loud)
- muted
true if the sound is muted else false
- loop
true if the sound is played in contiuous loop else false to play the sound only once (default)
- callback
A callback that will be called at the end of the sound
Warning
The loop and callback options are mutually exclusive. If the loop option is true the callback will never be called.
The callback function will be called with the following parameters:
- this
The sound object
Example:
friGame.resources.mySound.play({ callback: function () { friGame.resources.myOtherSound.play(); } });
-
sound.
stop
()¶ This function stops the sound if it is currently playing.
- Returns
The sound object
Example:
friGame.resources.mySound.stop();
-
sound.
pause
()¶ This function pauses the sound if it is currently playing.
- Returns
The sound object
Example:
friGame.resources.mySound.pause();
-
sound.
resume
()¶ This function resumes the sound if it is currently paused.
- Returns
The sound object
Example:
friGame.resources.mySound.resume();
-
sound.
tween
(properties[, options])¶ This function is enabled only if the frigame.fx.js plugin has been included before including the frigame.sound.js plugin.
For details on this function see
sprite.tween()
.The only possible value for the properties parameter is: volume.
Example:
friGame.resources.mySound.tween({ volume: 0.2 }, { duration: 'slow' });