The Babylon.js framework provides an audio engine based on WebAudio. It allows you to easily add 2D and 3D sounds using the provided tools that are developed for you by the Babylon.js team.
The Babylon.js framework provides a BABYLON.Sound
class. This class allows you to create and manage 2D and 3D sounds for your scene. To add a sound, the only thing you need to do is to create a new BABYLON.Sound
object, as follows:
var sound = new BABYLON.Sound("sound_name", "sound_file", scene);
You can now get access to methods such as .play
, .pause
, and .stop
.
In fact, the sounds are loaded asynchronously so that you cannot call sound.play()
right after creating the new sound object. This is why the BABYLON.Sound
constructor provides a readyToPlayCallback
parameter after the scene in order to handle the loading process. To play the sound when loaded, simply set the readyToPlayCallback
parameter, as follows:
var sound = new BABYLON.Sound("sound_name", "sound_file...