Skip to content

Audio

播放音乐或声音是任何游戏的重要组成部分。GDevelop为您提供了多种条件和操作以播放音频文件。

音频(音乐,声音)和计时器等在GDevelop中的持续时间都以秒为单位。

播放声音或音乐文件

注意

在可以播放声音/音乐之前,您必须首先确保玩家与游戏进行了交互(简单地点击/触摸屏幕即可解锁音频)。这是浏览器的限制,无法避免。在最终的游戏中,建议您在第一个场景上放置一个“按任意键或触摸屏幕继续”的消息。

播放音频文件的最简单方法是使用“播放声音”或“播放音乐文件”动作。选择要播放的文件作为两个动作的第一个参数。

支持的音频文件格式取决于平台,但通常有:

-波形音频格式(WAV),用于短暂的音效, -高级音频编码(AAC),用于背景音乐或大型音频文件。

某些平台还支持:

-MP3 - 但音质低于AAC(这在某些设备或操作系统上可能不受支持) -Ogg Vorbis (OGG) 但在iOS和Safari浏览器上无法工作。

提示

如果可能,我们强烈推荐使用AAC格式,因为不同浏览器和操作系统,包括iOS,均广泛支持AAC文件。

这些格式都可以用于音乐或音效,但强烈建议使用AAC(或MP3或OGG)用于背景音乐和大型音频文件。始终应将大型音频文件作为音乐而不是音效播放(这会将整个音频文件解码到内存中)。请阅读下一节了解加载和内存使用方面的区别。

当您选择音频文件时,它会被添加到游戏资源中。您可以打开资源编辑器选择是否在游戏启动时预加载音频 - 更多信息请参阅下一节。

选择“声音”或“音乐”操作

注意

正确理解声音和音乐之间的区别非常重要。否则,您的游戏性能和内存使用可能会受到影响。

GDevelop有两种不同的播放音频文件的方法。在引擎中,它们列为不同的操作:

-播放音效 或 -播放音乐

以及与这两个不同概念相关的操作和条件。以下是选择它们之间的经验法则:

-**短音频文件(如音效)**可以使用“播放声音”动作进行播放。它适用于短声音效,因为在播放之前它们将在内存中完全加载。 -大型音频文件和背景音乐必须使用“播放音乐”。它对大型音频文件的处理更好,因为它在内存中_流式传输_音频-结果是更少的内存使用。缺点是音乐需要在播放之前进行解码和流式传输,这导致第一次播放音乐时存在几毫秒的延迟。 Preloading an audio file as a sound effect or as music can help reduce the latency the first time music (or a sound effect) is played. See the next sections.

警告

小心不要在用来播放声音的操作中使用大型音频文件。整个音频文件将会被加载并解码到内存中,导致内存使用量非常大(可能达到几百兆字节甚至导致某些手机或低内存设备卡顿)。

在场景之间保持音乐和声音的播放

当一个新场景开始时,默认情况下,声音和音乐会停止。如果你希望保持它们播放,请打开场景的属性(在场景编辑器中右击场景)并取消勾选复选框:

20230303-230225.png

使用声道

使用“播放声音”或“播放音乐文件”操作时,音频文件会立即播放。播放结束后,它们会从内存中移除。循环播放是一个例外。当你将操作设置为循环播放音频文件时,它将无限循环播放。你必须使用“停止所有声音或音乐”操作来停止它们。

如果你想对声音进行更多的控制,请使用“在声道上播放声音”或“在声道上播放音乐文件”操作。该操作的使用方式相同,只是你需要输入一个声道号码。然后,该声道号码可以在其他操作或条件中重用。可以使用“声道号码”来检查是否在声道上播放声音,或者可以使用它来修改正在播放的音乐的某些属性。例如,你可以使用“声道号码”来根据玩家周围的敌人存在动态更新音乐的音量。

音量

声音和音乐默认以100%的音量播放,这是最大音量。当开始新的声音/音乐时,你可以输入不同的值(介于0和100之间)。

你还可以使用“游戏全局音量”操作来更改整个游戏的音频音量。值为0表示无法听到任何声音或音乐。这个操作在允许玩家静音或更改游戏音量时非常方便。例如,你的游戏中可能有一个访问游戏音量控制的设置屏幕。移动游戏通常还有一个静音游戏声音的按钮。

预加载:提高延迟和加载时间

当首次播放音乐或声音时,如果不预加载音频文件,则可能会出现准备音频文件时的延迟。这是因为设备必须:

  • 在磁盘/缓存中下载文件(如果游戏是Web游戏,则需要时间)
  • 解码并开始播放音频文件(对于音效,要么完全解码,要么通过流式传输渐进式解码)。

如果必须避免任何延迟,可以在资源面板中预加载音频文件。在资源列表中,点击音频文件并选择适当的选项: * Preload as sound: the audio file will be fully decoded in memory and ready to play,

  • Preload as music: the audio file will be ready to be streamed as a music,
  • Preload in cache: the audio file will be downloaded in cache by the browser (but not decoded) - this is only useful for web games.

Audio pre-loading options

Note

Another option is to 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 deemed to be stopped as it was unable to load).
  • Or you used the action to stop a sound or music on the channel.

Finding audio or making your own sounds

You can use the in-built sound effect creator tool called Jfxr for making sound effects: learn how to use Jfxr here.

You can also find high quality background musics and sound effects in the GDevelop Asset Store.

Reference

All actions, conditions and expressions are listed in the audio reference page.