Discuss Scratch

subnautica_subzero
Scratcher
8 posts

stopping songs?

how do I stop a song mid-way with using the “play until done” script?
Malicondi
Scratcher
1000+ posts

stopping songs?

you can have a seperate script to use either of these blocks:
stop all sounds
stop [other scripts in sprite v]
this will stop the sound. Hope this helps!

post #1000 post #100 i help in the forums post #1 post #500 0 second ninja
I recommend reading jvvg's essay about the scratch team before complaining, as it may change your opinion and provide insight on the topic.

coming soon :)


NuclearPhysics0
Scratcher
8 posts

stopping songs?

Are you talking about pausing/unpausing? If you're not, you can just use “stop all sounds” combined with “stop other scripts in this sprite”, but if you are talking about pausing/unpausing, then here are a few methods:

Method 1: This is probably the easiest method, but also the least efficient one. It slows the song down by 8 times, but doesn't completely pause it. However, if the person playing it unpauses it quickly after pausing it, then it may seem like it was paused. Here's the code:
when I receive [Pause v]
set [pitch v] effect to (-360) :: sound
set volume to (0) %

when I receive [Resume v]
set [pitch v] effect to (0) :: sound
set volume to (100) %
Now, the next method is more complex and takes longer for both you and the user, but it is FAR more efficient.

Method 2: This method stops the sound entirely, but tracks where the sound left off. It then plays the song, muted, as fast as possible until the sound reaches the point where it left off, then sets it to normal. The code looks like the following:
when green flag clicked
set [time v] to (0)
set [delta v] to (0)
set [last timer v] to (timer)
forever
set [delta v] to ((timer)-(last timer))
set [last timer v] to (timer)
if <unpaused :: operators> then
change [time v] by (delta)
end
if <condition to pause song :: operators> then
stop all sounds
stop [other scripts in sprite v]
broadcast [Pause v]
end
end

when I receive [Pause v]
set volume to (0) %

when I receive [Unpause v]
set [unpause timer v] to (0)
broadcast [Unpause 2 v]
play sound [whatever song v] until done

when I receive [Unpause 2 v]
set [pitch v] effect to (360) :: sound
set [unpause timer v] to (0)
repeat until <not <(time) < (unpause timer)>>
change [unpause timer v] by ((delta) * (8))
end
set volume to (100) %

So, although this code is long, basically, delta is the change in time per frame. I don't want to get into detail, so if you want to know more, just know that the max change in speed is setting the pitch to 360, and that speeds it up by 8 times. Now, knowing that, if you want, you can study the code above in more detail. However, if you don't want to do that, just copy it and put it in the project. I do highly recommend studying the code, though.

Last edited by NuclearPhysics0 (March 24, 2024 05:29:56)


"We all know that light travels faster than sound. That's why certain people appear bright until you hear them speak." - Albert Einstein
subnautica_subzero
Scratcher
8 posts

stopping songs?

wow, thanks for all of this. This will be a big help.

Powered by DjangoBB