Audio

Playing music or sounds is an essential part of any game. GDevelop provides you with several conditions and actions to play audio files.

Playing a sound or a musics

The easiest way to play an audio file is to use the action “Play a sound” or “Play a music file”. Choose the file to be played for the first parameter of both actions.

The difference between a sound and music is that music files are usually longer and larger audio files. Sounds are generally only a few seconds long. The audio file formats recommended for GDevelop are:

  • wav for sounds
  • mp3 for music.

If you distribute your game on the web, some browsers also support other file formats, for example ogg. Ogg is a superior alternative to mp3 for music. Unfortunately, it's not supported by all browsers. See more information on Wikipedia.

When you choose an audio file, it is added to the resources of the game, to be loaded during game startup. In an effort to minimize your game's size, be sure to remove audio files that are no longe in use by your game actions.

Using channels

When using “Play a sound” or “Play a music file” action, audio files are played immediately. On completion, they are removed from memory. Looping is an exception. When you set the action to loop the audio file, it will be played forever (Endless Loop). You must use an action to stop all sounds and/or music.

If you want more control over sounds, use the action “Play a sound on a channel” or “Play a music file on a channel”. The action is used in the same way, except that you have to enter a channel number. This channel number can then be reused in other actions or conditions. A “channel number” can be used to check to see if a sound is being played on a channel or it can be used to modify some properties of the music being played. For example, you could use a “channel number” to update the music volume dynamically according to the presence of enemies around the player.

Volume

Sounds and music are by default played with a volume of 100%, which is the maximum. When starting a new sound/music, you can enter a different value (between 0 and 100).

You can also use the action “Game global volume” to change the audio volume of the whole game. A value of 0 means that no sounds and or music can be heard. This action is convenient when allowing the player to mute or change the game's volume. For instance, you might have a settings screen in your game that accesses the volume control for the game. Mobile games usually have a button to mute the sounds of the game too.

Performance considerations

The first time music or sound is played, there can be a lag while the audio file is being prepared. If it's important for you to avoid any lag, you can play the audio or music file at the beginning of the scene where it's used. Set the initial volume 0. This will force the game to load the sound or music. The audio will be cached in memory. It will quickly load when called using your action.