Detect end of sound?

Is there a way to detect when a sound has ended? I want to play one sound, and then as soon as it ends play another sound. (What sound plays next will be determined by a variable.)

One method I haven’t tried yet because it seems overly complicated is to know the exact length of every sound and start a millisecond timer as soon as I start playing a sound, and do the next sound when the timer runs out, but I feel like it would be tricky to track both what sound is supposed to play next and its length. Unless… Is there a way to make a table? So I would use song.filename and song.length or something like that?

But anyway I feel like there should be a simpler way, like just a built in function called “ifSoundComplete” or something like that. Thanks!

1 Like

I never really use sounds,
but what you could do is,
for example, if a button was
clicked, play a certain sound,
and undo it using the code in
the wick file below. I hope it helps
Just Click Here :grin: .wick|attachment (28 KB)
The sounds I used weren’t long enough
(I don’t really have sound files) but it still
would work better with longer sounds.

If you just want the sound to stop somewhere
on its very own, what you could do is
make a clip of an object where the
sound will play and so would the frame until
it reaches a frame that has no sounds and only
the code “stop()”

These might sound simpler ways, but I do agree
with you, there should just be a way where sounds
could just stop when done, or just repeat when you
need them to!

1 Like

Thank you very much for your input! Unfortunately this doesn’t solve my problem. I’m fine with starting sounds, and with stopping all sounds. The issue I’m having is with knowing when a sound has played through completely and come to the end.

So for example, the background music of a game. What if you want to play the same song looping over and over? You have to know when the song has ended so you know when to start the song again. Or if you want another song in your soundtrack to start after the previous song has ended.

But I appreciate you going to the effort of setting up that example!

2 Likes

the best way that I can think of is to find the length of the bgm (background music) in frames (differs between fps), then make a counter that increments by one every frame. when the counter is complete, start the song again.

hope this helps you, and good luck!

Thank you for your advice! I will give that a try using ideas from the thread about waiting/ making a timer. I think it will be easiest if all the songs are the same length, but it’s probably possible with songs of different lengths if there are a lot more lines of code. Thanks for the luck!

Hi @mustbetuesday!

There’s a hidden option to loop a sound if you want, just do playSound(‘sound.wav’, {loop: true})

We’ll improve documentation so you can find these sorts of things!

-Nick

1 Like

Thank you @nick, this is definitely good to know! Documentation would be wonderful!

I take it there isn’t also a way to detect the end of the sound in general? (So I can do something other than loop at the end of the sound.)

Another thing that would be helpful would be to find out how far I am into the sound. An example of how I would use this is: Let’s say I have multiple versions of a song that sound similar to each other but with different instrumentation, so one is spookier, or icier, or more energetic. When the player moves into a different room, I would find out how far I am into the song, and start playing a different version of the song from the same point. So, if I was 1:02 into the normal version, I can start playing the spooky version from 1:02 in. I hope this makes sense! Thanks!

1 Like

@nick when you say “{loop: true}” do we just use “true” or use the whole thing?

@awc95014 use the whole thing, you’re adding an argument to the function playSound. You can also do something like playSound(“sound.wav”, {loop: true, volume: 0.5}) to play it at half volume and loop it.

2 Likes

@mustbetuesday Not sure about whether you can tell where in a sound file you are, there isn’t a way that I know of but if I find something I’ll let you know

It keeps saying ILLEGAL

What do you mean? Are you sure that the sound you’re playing is in the asset library?

I am sure the sound is in the asset library. however it keeps saying: “Unexpected Token ILLEGAL(Line 1)”

Can you send the Line 1 of code?

playSound(“Bumper Tag.mp3”, {loop: true, volume: 1});

wait, I retyped the ‘’. It’s working now.

2 Likes