Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
gdevelop5:all-features:audio [2018/10/15 23:29]
valiant_curiosity [Performance considerations]
gdevelop5:all-features:audio [2020/02/02 22:34] (current)
4ian
Line 1: Line 1:
 +
 +
 # Audio  # 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 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+## Playing a sound or a music file 
 + 
 +<note important>​Before a sound/music can be played, you have to first ensure that the player interacted with your game (a simple click/touch on the screen is ok to unlock audio). This is a limitation of browsers and can not be worked around. In your final game, it's recommended to have on your first scene a "Press any key or touch the screen to continue"​ message.</​note>​
  
 The easiest way to play an audio file is to use the action "​**Play a sound**"​ or "​**Play a music file**"​. The easiest way to play an audio file is to use the action "​**Play a sound**"​ or "​**Play a music file**"​.
Line 13: Line 17:
 * `mp3` for music. * `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 [[https://​en.wikipedia.org/​wiki/​HTML5_audio#​Supported_audio_coding_formats|more information on Wikipedia]].+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 [[https://​en.wikipedia.org/​wiki/​HTML5_audio#​Supported_audio_coding_formats|more information on Wikipedia]].
  
 <​note>​When you choose an audio file, it is added to the resources of the game, to be loaded during game startup. <​note>​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.+In an effort to minimize your game's size, be sure to remove audio files that are no longer ​in use by your game actions.
 </​note>​ </​note>​
 +
 +## Keep musics and sounds playing between scenes
 +
 +By default, sounds and musics are stopped when a new scene is started. If you want to keep them playing, open the properties of the scene (right click on the scene, in the scene editor) and uncheck the checkbox:
 +{{ :​gdevelop5:​all-features:​scene_properties.png?​nolink |}}
 +
  
 ## Using channels ## Using channels
Line 23: Line 33:
 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. ​ 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.+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 channelor 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 ## Volume
Line 34: Line 44:
  
 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. 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.
 +
 +## More about the state of a sound/music
 +
 +When you use the action to **play a sound or music on a channel**, it's put in the state **"​playing"​**,​ even if still **loading** in memory. ​
 +
 +The condition "A sound is being played"​ (on this channel) is then true, while "A sound is stopped"​ (on this channel) will be false, as well as "A sound is paused"​ (on this channel again).
 +When the sound is fully loaded, it will start to be really played on your device (so you will hear the audio). ​
 +
 +The sound will be stopped when:
 +
 +* It reaches its **end** and is not configured to loop.
 +* There is **an error during the loading** (in which case it will be considered as playing for a few milliseconds,​ then will be considered as stopped as it was unable to load).
 +* Or you used **the action** to stop a sound or music on the channel.