Welcome back to the ongoing Defold Game Engine tutorial series looking at all aspects of game development using the Defold game engine. In this tutorial we are going to look at the process of playing sound and music in Defold.
There is an HD video version of this tutorial available here.
Playing sound in the Defold engine is simple, which is a double edged sword. It’s extremely easy to add and play a sound file. On the other hand the functionality provided is also quite simple. Many things you may want to accomplish ( callback on sound end, fast forwarding/rewinding a playing audio file, positional audio, etc. ) simply aren’t supported.
Let’s start by adding some audio files to the project. In my case I added a folder named audio and dragged in an ogg file and a wav file from Windows Explorer to Defold. These are the two file formats supported. The ogg file is an Ogg Vorbis file, a compressed streamed format similar to mp3 format but not patent encumbered. Wav files are almost entirely uncompressed but require less processing power. In a nutshell for longer form audio files, such as music, use an ogg. For sound effects, use wav. The end result should look something like:
Next we go ahead and create a sound component for our game. You could parent your sound components to whatever game object you wish. If it makes sense to create a music player game object and attach all the sounds to it, you could. Or if you want your game objects (for example, the player) to own their own sound components, that’s alright too. Select whatever game object you want to add the sound to, right click and select Add Component.
Next select Sound from the component list.
Here I am creating one for the music file first:
Hit the … button beside Sound and select the ogg file you dropped in earlier. Notice in this case I changed the Group to music. I repeat the same process for the wav file, instead named sfx.
Now playing the sound effect using code is extremely easy and once again uses messages like so:
msg.post("#music","play_sound")
Simply post the play_sound message to the music component of our current game object. Playing the sound effect file is the exact same process but instead post the message to the #sfx component like so:
msg.post("#sfx","play_sound")
We can also control the volume of the sound component, like so:
msg.post("#music","set_gain", { gain = volume })
Volume is a value from 0.0 to 1.0, where 0.0 is no volume, while 1.0 represents full volume. Remember earlier when I changed the group for our music file from “master” to “music”. Groups enable you to control several sound effects at once, for example muting all the playing songs or sound effects. For example, we could mute all the sounds in the group music with the following call:
sound.set_group_gain(hash("music"), 0.0)
Here is the full source for this example:
-- Local variable for volume. 0 is none, 1 is full local volume = 1.0 function init(self) msg.post(".","acquire_input_focus") -- start playing the background music msg.post("#music","play_sound") end function on_input(self, action_id, action) if(action_id == hash("SPACE_PRESSED") and action.pressed == true) then -- if user hits the spacebar play the soundfx file. -- multiple presses will play multiple instances. msg.post("#sfx","play_sound") elseif(action_id == hash("ESC") and action.pressed == true) then -- on ESC mute the entire group "music" sound.set_group_gain(hash("music"), 0.0) elseif(action_id == hash("DOWN_ARROW") and action.pressed == true) then if volume >= 0 then volume = volume - 0.1 end -- lower the volume of our music msg.post("#music","set_gain", { gain = volume }) elseif(action_id == hash("UP_ARROW") and action.pressed == true) then if volume <= 1 then volume = volume + 0.1 end -- increase the volume of our music msg.post("#music","set_gain", { gain = volume }) end end
The Video