Play, Pause and Resume Music

Before proceeding, please use the forum search feature at the top of the page to check if your question has already been answered.

How do I…

Play, then pause, then resume a music file on a single channel by pressing/releasing the same key for each.

What is the expected result

In my example from the screenshot, I believe that when a player releases the “p” key, my music file “Weezin’” should play. And then while the file is playing, if the “p” key is released again, the music should pause. And then if it is released once more, the music should resume.

What is the actual result

What actually happens is that the “p” key just restarts the music file with each release.

Related screenshots

Project files (optional)

I tried to include a .zip project file, but I have not done that before and could not figure out how to insert it here. Thank you.

1 Like

Check if same thing happens with play SOUND on channel

1 Like

Use something like this instead.


I just tested it an it works perfectly. You can use play music on channel but it’s usually better to use play a sound.

If you want it to restart on the fourth click, change the last event to 0 instead of 1.

1 Like

You need a variable so that all sub events are not instantly executed one after the other.

https://forum.gdevelop.io/search?q=toggle%20button

2 Likes

You’ve created a chain reaction or domino effect. The events are executed from top to bottom. Each action is making the next condition true. It’s a common mistake.

if music is not paused and not playing then play
if playing then pause
if paused then resume

The play music isn’t as reliable as the play sound. The play sound is the recommended method.

There are multiple ways of doing this. For 2 states toggling a Boolean variable works. For more than 2 states, I’ll sometimes use a Boolean variable to track when a change has been made.

or

2 Likes

Man, you guys all gave excellent solutions. Cannot thank you enough. A bunch of rockstars you are!

3 Likes