Looping Background Music With Intro in Godot Engine
Tags: tutorials, godot
Published: 2021-04-22 20:43:10

---
title: Looping Background Music With Intro in Godot Engine
published: true
date: 2020-09-12 18:37:24 UTC
tags: Tutorials,godot
canonical_url: https://www.oddevan.com/2020/looping-background-music-with-intro-in-godot-engine/
---

Title is a mouthful, right? But this is something I’ve been trying and failing to find a clear answer to for months. Now that I’ve found the answer, time to take [Sal](https://salferrarello.com/)‘s advice and write a blog post.

## The Basic Idea

You’re making a video game. You’re using [Godot](https://godotengine.org/) because you like open source and free stuff. You’ve got some kick-awesome background music because, well, reasons. And while you can get a good loop out of the music, you’d still like to have an introduction to the music.

Background music with an intro is pretty standard in most video game music. Take a listen to “Green Hill Zone” from… well, every Sonic game ever. But particularly Sonic Mania.

<iframe loading="lazy" title='Sonic Mania "Green Hill Zone Act 1" Music' width="580" height="326" src="https://www.youtube.com/embed/8lkTRBywniw?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>

Notice how when the music loops at 0:53, it doesn’t go all the way back to the beginning but instead loops from partway into the music? This helps set the stage for the level’s atmosphere and provide a more natural feel to the music.

It’s also a feature we’ve come to expect as players, so if you can do it in your game it’s a good idea. So how do we get it in Godot?

## Preparing Your Music

For my game, I’m using [“1977” by Adam Young](https://www.youtube.com/watch?v=Ewegy6MC9pY) as the background music. I will, of course, need to replace it with something officially licensed (or [original](http://madcrasher.com/)) for the finished product. I used my copy of [Ableton Live](http://ableton.com/) to process the audio in order to get a clean-ish loop and saved it as an [Ogg Vorbis](https://xiph.org/vorbis/) file to be imported into Godot.

## Import into Godot

The key info for the loop is kept on the file itself. We want the file to loop (obviously), and we want to set the loop offset at where we want the looping portion of the file to begin. **This value is in seconds!**

![](https://i1.wp.com/www.oddevan.com/wp-content/uploads/2020/09/1977-import.png?resize=254%2C498&ssl=1)

Now for the unintuitive part: **do not auto-play the music**.

![](https://i2.wp.com/www.oddevan.com/wp-content/uploads/2020/09/1977-inspector.png?resize=264%2C438&ssl=1)

Selecting Autoplay here will start playback at the loop offset, which will skip the intro. That’s the exact thing we’re trying to avoid!

Instead of autoplaying, we need to add a line to the root node’s script:

![](https://i2.wp.com/www.oddevan.com/wp-content/uploads/2020/09/1977-code.png?resize=580%2C86&ssl=1)

What’s the difference? We’re using the `play` method in the AudioStreamPlayer class. This method uses an optional parameter to indicate where to start playing from. We want to start playing from the beginning, so we pass `0` to the method.

The end result should be background audio that starts playing at the beginning of the file but loops over only one specific part of it.

Hope this helps! Leave a comment if you’ve got feedback.