Discuss Scratch
- Discussion Forums
- » Questions about Scratch
- » Play sound for xy seconds
- Herr_Smit
-
Teacher
3 posts
Play sound for xy seconds
Hi,
I'm quite new to Scratch and so far I have only been using what's available at scratch.mit.edu (blocks, sounds etc.). I'm a teacher at a primary school and I'm currently following a tutorial into Scratch on edX.
I uploaded eight sounds, each of which contain a single note. I want my student to create a sequence which plays the melody Frère Jacques / Are you sleeping brother John. This involves two tasks:
Here ist a link to the project: https://scratch.mit.edu/projects/325947216/editor
I have already achieved what I want, but with three blocks for each note (Start sound, Wait xy seconds, Stop all sounds). Is there another, shorter way?
Thanks in advance & kind regards
Thorsten
I'm quite new to Scratch and so far I have only been using what's available at scratch.mit.edu (blocks, sounds etc.). I'm a teacher at a primary school and I'm currently following a tutorial into Scratch on edX.
I uploaded eight sounds, each of which contain a single note. I want my student to create a sequence which plays the melody Frère Jacques / Are you sleeping brother John. This involves two tasks:
- Create the sequence do re mi do do re mi do mi fa so mi fa so etc.
- Play the first ten notes for each one second, the eleventh note for two seconds etc.
Here ist a link to the project: https://scratch.mit.edu/projects/325947216/editor
I have already achieved what I want, but with three blocks for each note (Start sound, Wait xy seconds, Stop all sounds). Is there another, shorter way?
Thanks in advance & kind regards
Thorsten
- LegoManiac04
-
Scratcher
1000+ posts
Play sound for xy seconds
Use Custom Blocks
Now, you can do this:
Hope this helps!
define Play (Sound) (For This Long)
start sound (Sound):: sound
wait (For This Long) secs
stop all sounds
Now, you can do this:
Play () ():: custom // In the first box, you type in the name of the sound, and in the second how long the sound will play.
Hope this helps!

- UnconstructivePoster
-
New Scratcher
100+ posts
Play sound for xy seconds
Your project isn't shared. Can you share it so that we can view it?
As far as I know, there's no simpler way to play a sound for a certain amount of time before stopping it. If you want to make it easier, you could try making a custom block definition (choose “make a block”) for playing the sounds, so that afterwards, you would only need to use one block (the custom block) each time, instead of three. Alternatively, you could just use Scratch's built-in noteblocks - click on “extensions” in the bottom left corner and choose “music” - though the sounds might be of lower quality.
EDIT: ninja'd
As far as I know, there's no simpler way to play a sound for a certain amount of time before stopping it. If you want to make it easier, you could try making a custom block definition (choose “make a block”) for playing the sounds, so that afterwards, you would only need to use one block (the custom block) each time, instead of three. Alternatively, you could just use Scratch's built-in noteblocks - click on “extensions” in the bottom left corner and choose “music” - though the sounds might be of lower quality.
EDIT: ninja'd
Last edited by UnconstructivePoster (Aug. 28, 2019 17:03:29)
- Herr_Smit
-
Teacher
3 posts
Play sound for xy seconds
Thanks, LegoManiac04 and UnconstructivePoster!
I hadn't known about custom blocks yet. I will try to use that!
P. S. The project is now shared.
I hadn't known about custom blocks yet. I will try to use that!
P. S. The project is now shared.
- openPoll
-
Scratcher
1000+ posts
Play sound for xy seconds
If you click on the blue “+” button in the bottom-left button of the editor, you can use the music blocks for your project. You can control how long each block's sound lasts for.
- Bigfuzzybadger
-
Scratcher
70 posts
Play sound for xy seconds
You can crop sounds in the sound editor to the desired length if that helps?
- Tutor-42
-
Scratcher
100+ posts
Play sound for xy seconds
You may be interested in the Music extension!
Click the icon that includes the + sign at the very bottom left of editor well below all the block types.
Select the very first extension which is Music.
You can then use the “Play note … for … beats” block.
Each note is assigned a number eg. C is 60.
I have written a custom block for you in this project: https://scratch.mit.edu/projects/326467178/
It would let students code “Play Note Re 0.8”, “Play Note Mi 1.6” etc
I have coded the start of Frère Jacques.

Edit: a couple more ideas:
You could make a separate custom block for each note if you prefer.
eg. Play Do (time), Play Re (time) etc
Then students could not make spelling mistakes for the notes Do, Re, Mi etc as they would not have to enter them.
** I have added these custom blocks into the project **
Alternatively in the example project you could just check “if letter(1) of note = D” , “if letter(1) of note = R” , etc so students only need to get the first letter of each note correct when they enter them.
Also you could add code at the top of the custom block(s), “if time = 0 then set time to 1”, as playing a note for 0 seconds doesn't do much.
Note that the project uses the “play note … for … beats” block in the music extension.
You can also set the tempo which defaults to 60 beats per minute, ie. 1 beat per second.
The input I called “time” is actually the time in beats.
Click the icon that includes the + sign at the very bottom left of editor well below all the block types.
Select the very first extension which is Music.
You can then use the “Play note … for … beats” block.
Each note is assigned a number eg. C is 60.
I have written a custom block for you in this project: https://scratch.mit.edu/projects/326467178/
It would let students code “Play Note Re 0.8”, “Play Note Mi 1.6” etc
I have coded the start of Frère Jacques.

Edit: a couple more ideas:
You could make a separate custom block for each note if you prefer.
eg. Play Do (time), Play Re (time) etc
Then students could not make spelling mistakes for the notes Do, Re, Mi etc as they would not have to enter them.
** I have added these custom blocks into the project **
Alternatively in the example project you could just check “if letter(1) of note = D” , “if letter(1) of note = R” , etc so students only need to get the first letter of each note correct when they enter them.
Also you could add code at the top of the custom block(s), “if time = 0 then set time to 1”, as playing a note for 0 seconds doesn't do much.
Note that the project uses the “play note … for … beats” block in the music extension.
You can also set the tempo which defaults to 60 beats per minute, ie. 1 beat per second.
The input I called “time” is actually the time in beats.
Last edited by Tutor-42 (Sept. 2, 2019 09:33:02)
- Herr_Smit
-
Teacher
3 posts
Play sound for xy seconds
Thank you, Totor-42! This is fantastic! I hadn't noticed your post until just now. I will give your custom block a deeper look later.
- Discussion Forums
- » Questions about Scratch
-
» Play sound for xy seconds