DEV Community

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

Posted on

16

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!

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay