Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Making a sound stop when another sound plays
- JLM628
-
11 posts
Making a sound stop when another sound plays
I'm trying to make a project (that involves a drum kit) where when you press a key, it plays an open hi-hat sound. But when you press another key, that is also a closed hi-hat sound, I wanna make it so the open hi-hat stops as soon as you play the closed hi-hat. Does anyone know how to do something like this?
- awesome-llama
-
1000+ posts
Making a sound stop when another sound plays
There are 3 blocks that can stop a sound (as far as I know)…
You have a few options using the above blocks.
You could:
set volume to (0) %
stop all sounds
stop [ v]
You have a few options using the above blocks.
You could:
- Set the volume to 0. (note that the sound will still play, you just won't hear it)
- Use the stop all sounds block, pretty self-explanatory.
- Use the stop block. If you are using a play until done and you stop the script that contains the block (using either “other scripts in sprite” or “all”), the sound will be stopped. The “all” option functions as a stop all sounds block.
- Edit: another option is to play the sound in a clone, deleting it will stop the sound.
Last edited by awesome-llama (July 18, 2022 08:49:05)
- blackspider222
-
50 posts
Making a sound stop when another sound plays
Please send me the project link Because i dont understand this really much. But if it is what i think it is, The code is this:
And in another sprite, this is the script (for an example)
when green flag clicked
play sound [ur sound v]
And in another sprite, this is the script (for an example)
when green flag clicked
wait (Whatever number) secs
stop all sounds
play sound [Your same music v]
Last edited by blackspider222 (July 18, 2022 08:51:50)
- Skmish
-
85 posts
Making a sound stop when another sound plays
play sound [Drum v]
repeat until <<key [Any] pressed?>>end
when it plays the sound, add a repeat until loop to go with the sound so it forever plays until another key is pressed?
and when key pressed (Any) it will start the other sound instead
- RT_Borg
-
1000+ posts
Making a sound stop when another sound plays
Hi JLM628,
I have a demo that does pretty much what you're looking for.
Demo: Background Sound + Sound Effects
https://scratch.mit.edu/projects/702932634/
One background song plays uninterrupted (so just using “stop all” wasn't a solution in my case) while sound effects can interrupt one another.
It uses clones (one of the suggestions from @awesome-llama ). But I recall deleting the clone wasn't enough, and it required setting the volume to 0 first. The sound actually played to completion in a deleted clone.
– RT_Borg
I have a demo that does pretty much what you're looking for.
Demo: Background Sound + Sound Effects
https://scratch.mit.edu/projects/702932634/
One background song plays uninterrupted (so just using “stop all” wasn't a solution in my case) while sound effects can interrupt one another.
It uses clones (one of the suggestions from @awesome-llama ). But I recall deleting the clone wasn't enough, and it required setting the volume to 0 first. The sound actually played to completion in a deleted clone.
– RT_Borg
Last edited by RT_Borg (July 18, 2022 11:41:22)
- JLM628
-
11 posts
Making a sound stop when another sound plays
So you all understand what I'm talking about, this is the project I'm trying to do this too.
https://scratch.mit.edu/projects/715053063/
https://scratch.mit.edu/projects/715053063/
- JLM628
-
11 posts
Making a sound stop when another sound plays
In fact, I have an idea: Remix the project and see if you can pull it off. Then I'll try and see if I can do it.
- RT_Borg
-
1000+ posts
Making a sound stop when another sound plays
It seems like playing instruments with clones, and broadcasting a message to stop when another instrument should interrupt them, is exactly what you're looking for. Like I showed in my demo, 3 messages up.
If there's something unclear about how the demo code works, or you're having trouble doing the same in your code, feel free to ask questions.
– RT_Borg
If there's something unclear about how the demo code works, or you're having trouble doing the same in your code, feel free to ask questions.
– RT_Borg
- JLM628
-
11 posts
Making a sound stop when another sound plays
@RT_Borg If I have to be honest, this code looks really complicated.
- RT_Borg
-
1000+ posts
Making a sound stop when another sound plays
Hi JLM628, I agree they made it more difficult that it should be to stop just a single sound while other sounds keep playing.
If it's ok to stop all the sounds in your project when the new one plays, things are easy–just use the “stop all sounds” block.
But if you need other sounds to keep playing while one or more is interrupted, the work-around is to play sounds in clones, which each have their own volume control. So you set up the sound a clone should play and create a new clone. The clone starts playing its sound when it's created. You need to be able to tell the clone when it's time to stop playing (which means setting its volume to 0 and deleting itself) which takes a broadcast. (The other way to communicate with a clone is using a “For all sprites” variable, which may be easier or harder to understand, depending on which features of Scratch you're more comfortable coding with.)
If you have any questions about how the demo works, just ask. Or if you have trouble doing something similar in your own project, show us when you've tried something and we can help if there's a bug you can't spot.
– RT_Borg
If it's ok to stop all the sounds in your project when the new one plays, things are easy–just use the “stop all sounds” block.
But if you need other sounds to keep playing while one or more is interrupted, the work-around is to play sounds in clones, which each have their own volume control. So you set up the sound a clone should play and create a new clone. The clone starts playing its sound when it's created. You need to be able to tell the clone when it's time to stop playing (which means setting its volume to 0 and deleting itself) which takes a broadcast. (The other way to communicate with a clone is using a “For all sprites” variable, which may be easier or harder to understand, depending on which features of Scratch you're more comfortable coding with.)
If you have any questions about how the demo works, just ask. Or if you have trouble doing something similar in your own project, show us when you've tried something and we can help if there's a bug you can't spot.
– RT_Borg
- JLM628
-
11 posts
Making a sound stop when another sound plays
1. I really wish they had an easier way to make it so one audio plays while another one stops. Hi JLM628, I agree they made it more difficult that it should be to stop just a single sound while other sounds keep playing.
If it's ok to stop all the sounds in your project when the new one plays, things are easy–just use the “stop all sounds” block.
But if you need other sounds to keep playing while one or more is interrupted, the work-around is to play sounds in clones, which each have their own volume control. So you set up the sound a clone should play and create a new clone. The clone starts playing its sound when it's created. You need to be able to tell the clone when it's time to stop playing (which means setting its volume to 0 and deleting itself) which takes a broadcast. (The other way to communicate with a clone is using a “For all sprites” variable, which may be easier or harder to understand, depending on which features of Scratch you're more comfortable coding with.)
If you have any questions about how the demo works, just ask. Or if you have trouble doing something similar in your own project, show us when you've tried something and we can help if there's a bug you can't spot.
– RT_Borg
2. If I was to hit the cymbals in my project, then go from open to a closed hi-hat, I couldn't use “stop all sounds” because it would stop the cymbals also.
3. How did you even get the “when I start as a clone” block?
4. How do you make that volume thing that you have in your project?
Last edited by JLM628 (July 20, 2022 04:25:23)
- abcde1234qwe
-
500+ posts
Making a sound stop when another sound plays
1 - You can suggest it in the Suggestions discussion. 1. I really wish they had an easier way to make it so one audio plays while another one stops.
2. If I was to hit the cymbals in my project, then go from open to a closed hi-hat, I couldn't use “stop all sounds” because it would stop the cymbals also.
3. How did you even get the “when I start as a clone” block?
4. How do you make that volume thing that you have in your project?
stop this sound::sound
stop [music v]::sound
3 - The When I Start As A Clone block is in Control.
when I start as a clone
4 - Create a variable for all sprites named VOLUME. Show the sprite and right click or double click it in the stage. Then, select it to be a slider. Set the slider range from 0 to 100.
when green flag clicked
forever
set volume to (VOLUME::variables) %
end
Last edited by abcde1234qwe (July 20, 2022 05:16:25)
- RT_Borg
-
1000+ posts
Making a sound stop when another sound plays
Hi JLM628, I think abcde1234qwe answered most of your points.
If you really only need one single pair of sounds to interrupt one another, and all the other sounds should play their whole length, I can it a little simpler.
For the sake of this example, I'll suppose the sounds are “Open Hi-Hat” and “Closed Hi-Hat”, and the keys are O and C.
make a variable with the “For This Sprite Only” box checked.
When I say "must match the sound exactly", that includes capitalization.
That should do what you're looking for.
Feel free to ask more explanation if anything I or abcde1234qwe wrote isn't clear yet.
– RT_Borg
If you really only need one single pair of sounds to interrupt one another, and all the other sounds should play their whole length, I can it a little simpler.
For the sake of this example, I'll suppose the sounds are “Open Hi-Hat” and “Closed Hi-Hat”, and the keys are O and C.
make a variable with the “For This Sprite Only” box checked.
(clone-sound) // For this sprite only
when [O v] key pressed
broadcast [Stop All High Hat Sounds v] and wait
set [clone-sound v] to [Open Hi-Hat] // must match the sound *exactly*
create clone of [myself v] // will play the clone-sound when it's created
when [C v] key pressed
broadcast [Stop All High Hat Sounds v] and wait
set [clone-sound v] to [Closed Hi-Hat] // must match the sound *exactly*
create clone of [myself v] // will play the clone-sound when it's created
set [clone-sound v] to [] // empty - no value
When I say "must match the sound exactly", that includes capitalization.
when I start as a clone
play sound (clone-sound)
when I receive [Stop All High Hat Sounds v]
if <(clone-sound) = [Open Hi-Hat]> then
set volume to (0) % // only clones will have clone-sound = Open Hi-Hat
delete this clone
end
if <(clone-sound) = [Closed Hi-Hat]> then
set volume to (0) % // only clones will have clone-sound = Closed Hi-Hat
delete this clone
end
That should do what you're looking for.
Feel free to ask more explanation if anything I or abcde1234qwe wrote isn't clear yet.
– RT_Borg
Last edited by RT_Borg (July 20, 2022 06:13:53)
- JLM628
-
11 posts
Making a sound stop when another sound plays
I think my biggest problem is that I don't have any sprites in the project I linked… Hi JLM628, I think abcde1234qwe answered most of your points.
If you really only need one single pair of sounds to interrupt one another, and all the other sounds should play their whole length, I can it a little simpler.
For the sake of this example, I'll suppose the sounds are “Open Hi-Hat” and “Closed Hi-Hat”, and the keys are O and C.
make a variable with the “For This Sprite Only” box checked.(clone-sound) // For this sprite onlywhen [O v] key pressed
broadcast [Stop All High Hat Sounds v] and wait
set [clone-sound v] to [Open Hi-Hat] // must match the sound *exactly*
create clone of [myself v] // will play the clone-sound when it's created
when [C v] key pressed
broadcast [Stop All High Hat Sounds v] and wait
set [clone-sound v] to [Closed Hi-Hat] // must match the sound *exactly*
create clone of [myself v] // will play the clone-sound when it's created
set [clone-sound v] to [] // empty - no value
When I say "must match the sound exactly", that includes capitalization.when I start as a clone
play sound (clone-sound)when I receive [Stop All High Hat Sounds v]
if <(clone-sound) = [Open Hi-Hat]> then
set volume to (0) % // only clones will have clone-sound = Open Hi-Hat
delete this clone
end
if <(clone-sound) = [Closed Hi-Hat]> then
set volume to (0) % // only clones will have clone-sound = Closed Hi-Hat
delete this clone
end
That should do what you're looking for.
Feel free to ask more explanation if anything I or abcde1234qwe wrote isn't clear yet.
– RT_Borg
- JLM628
-
11 posts
Making a sound stop when another sound plays
Okay, but how do you change the volume in the project? I've got the block, but how do I change the volume of the project?1 - You can suggest it in the Suggestions discussion. 1. I really wish they had an easier way to make it so one audio plays while another one stops.
2. If I was to hit the cymbals in my project, then go from open to a closed hi-hat, I couldn't use “stop all sounds” because it would stop the cymbals also.
3. How did you even get the “when I start as a clone” block?
4. How do you make that volume thing that you have in your project?stop this sound::sound
stop [music v]::sound
3 - The When I Start As A Clone block is in Control.when I start as a clone
4 - Create a variable for all sprites named VOLUME. Show the sprite and right click or double click it in the stage. Then, select it to be a slider. Set the slider range from 0 to 100.when green flag clicked
forever
set volume to (VOLUME::variables) %
end
- cIoudyness
-
500+ posts
Making a sound stop when another sound plays
volume acts like a local variable. if you wanted to set the volume of the entire project you might want to set the volume of every entity to a variable, maybe a slider?
- RT_Borg
-
1000+ posts
Making a sound stop when another sound plays
I think my biggest problem is that I don't have any sprites in the project I linked…
Ah. Yes, to do what you want, you'll have to make a sprite (called Studio, or Sounds, or Drum Kit… whatever you want), drag the scripts from the stage editing area on to the Studio (where it shows up in the lower right sprite area) and drag the sounds in from the Sounds tab. (And of course then remove the duplicate scripts from the stage.) Hopefully right clicking on the code edit area of your new sprite and choosing “clean up” will arrange the scripts well.
File/Save As Copy to try it out without modifying the original.
Okay, but how do you change the volume in the project? I've got the block, but how do I change the volume of the project?
I see you have the Volume variable showing. Right-click and choose “slider”, or double-click to rotate between the options. When the slider displays, it defaults to 0-100 (which is perfect for volume) but if you ever need a different range, while the slider is showing, right-click on it and choose “change slider range”.
Also to note, there is no “project volume”. The stage, and every sprite (and every clone of every sprite) has its own independent volume. That's why the trick I showed works–to create a clone and set it's volume to 0 when it should be interrupted.
If you have multiple sprites (and stage) all playing sounds in a project, and you want the slider to control all of their volumes, they each need that loop:
when green flag clicked
forever
set volume to (Volume::variables) %
If you have clones that play sounds, they begin at whatever volume the sprite they were cloned from had when they were created. For clones that stick around for a while, you (normally) would also want them to change volume when the slider changes:
when I start as a clone
forever
set volume to (Volume::variables) %
But for these interruptible instrument clones we're talking about, DON'T put a “when I start as a clone” block like that, because their whole purpose is to be able to set their volume to 0 at the right time and have it stay there.
– RT_Borg
Last edited by RT_Borg (July 20, 2022 22:49:45)
- JLM628
-
11 posts
Making a sound stop when another sound plays
I added the slider thing, but now there's no sound!! What am I doing wrong????I think my biggest problem is that I don't have any sprites in the project I linked…
Ah. Yes, to do what you want, you'll have to make a sprite (called Studio, or Sounds, or Drum Kit… whatever you want), drag the scripts from the stage editing area on to the Studio (where it shows up in the lower right sprite area) and drag the sounds in from the Sounds tab. (And of course then remove the duplicate scripts from the stage.) Hopefully right clicking on the code edit area of your new sprite and choosing “clean up” will arrange the scripts well.
File/Save As Copy to try it out without modifying the original.Okay, but how do you change the volume in the project? I've got the block, but how do I change the volume of the project?
I see you have the Volume variable showing. Right-click and choose “slider”, or double-click to rotate between the options. When the slider displays, it defaults to 0-100 (which is perfect for volume) but if you ever need a different range, while the slider is showing, right-click on it and choose “change slider range”.
Also to note, there is no “project volume”. The stage, and every sprite (and every clone of every sprite) has its own independent volume. That's why the trick I showed works–to create a clone and set it's volume to 0 when it should be interrupted.
If you have multiple sprites (and stage) all playing sounds in a project, and you want the slider to control all of their volumes, they each need that loop:when green flag clicked
forever
set volume to (Volume::variables) %
If you have clones that play sounds, they begin at whatever volume the sprite they were cloned from had when they were created. For clones that stick around for a while, you (normally) would also want them to change volume when the slider changes:when I start as a clone
forever
set volume to (Volume::variables) %
But for these interruptible instrument clones we're talking about, DON'T put a “when I start as a clone” block like that, because their whole purpose is to be able to set their volume to 0 at the right time and have it stay there.
– RT_Borg
Edit: Nvm, I just had to reload the page.
Last edited by JLM628 (July 21, 2022 06:36:25)
- Pro_Guy99
-
1 post
Making a sound stop when another sound plays
I figured out what you need to do after learning that deleting clones stop music.
This detects what song is playing, you can modify this to be a clicked sprite or really anything you want, it all works the same.
I'm not that good at this stuff, but it worked for me (see https://scratch.mit.edu/projects/1188098716/ )
This detects what song is playing, you can modify this to be a clicked sprite or really anything you want, it all works the same.
when green flag clickedThis will play the sound, change the wait value to (I suggest) a little over the duration of the sound
set [ song 1 playing?] to [0]
set [ song 2 playing?] to [0]
forever
if <key [ a] pressed?> then
set [ song 1 playing?] to [1]
set [ song 2 playing?] to [0]
create clone of [ myself]
end
if <key [ b] pressed?> then
set [ song 1 playing?] to [0]
set [ song 2 playing?] to [1]
create clone of [ myself]
end
end
when I start as a cloneThis deletes the clone on the input or whatever detection method you use.
hide
if <(song 1 playing?) = [1]> then
forever
play sound [ song 1] until done
wait (999) secs
end
else
if <(song 2 playing?) = [1]> then
forever
play sound [ song 2] until done
wait (999) secs
end
else
end
end
when I start as a clone
if <(song 1 playing?) = [1]> then
wait until <not <(song 1 playing?) = [1]>>
delete this clone
else
if <(song 2 playing?) = [1]> then
wait until <not <(song 2 playing?) = [1]>>
delete this clone
else
end
end
I'm not that good at this stuff, but it worked for me (see https://scratch.mit.edu/projects/1188098716/ )
- myartisbeautilful123
-
2 posts
Making a sound stop when another sound plays
hey guys, so uhh i think i found a pretty good solution(not sure if u need it anymore but..) basically u create a new sprite then make it clone, then said clone will start playing the sound, if u want to disable it then delete the clone\
idk if this works as i havent tested this but just a suggestion
idk if this works as i havent tested this but just a suggestion

- Discussion Forums
- » Help with Scripts
-
» Making a sound stop when another sound plays