DEV Community

Cover image for Loop audio in Godot
Eduardo Julião
Eduardo Julião

Posted on

Loop audio in Godot

In this article, I'm going to show you one way to loop music in Godot!

But first, we need some software installed before we start.

Working on the audio with Audacity

Open Audacity and import the audio file, you should see something like this:

Image description

In the bottom panel, click on the cog wheel and select Start and End of the Selection:

Image description

And on the little drop down to the right, select samples (Godot works with samples):

Image description

We have the information we need! Now, in Audacity, go to File > Export > Export as WAV:

Image description

Creating a scene

Let's start by creating a new 2D Scene.

Image description

Adding audio to Godot

Grab the .wav file we just exported and add to the res:// folder of your project:

Image description

Now, with your .wav file selected, click in the Import tab in the panel above your res:// folder, let's change some of the properties.
Set Loop Mode to Foward and Loop Begin and Loop End to the sample values found in Audacity. Then, click Reimport(*):

Image description

Adding Audio Stream to the scene

In the scene tab, right click Node2D and add a new child node of
the typeAudioStreamPlayer2D:

Image description

Image description

Select the newly created AudioStreamPlayer2D under Node2D and this will open an Inspector for this node. In the Stream property of your audio player, drag and drop the .wav file, make sure Autoplay is enabled.

Image description

Testing

Save your scene and press F6 to run the current scene (or press this button).

Image description

Now, your audio will play normally, but will loop between the samples you've set int the Loop Begin and End!

Thank you

Switching engines is always a challenge, but learning Godot for the past few days feels great, the engine is lightweight, and GDScript is a delight to work with.

Hope you find this article helpful!

See you in the next one!

Top comments (0)