Please update your browser
Your browser version is not supported by this site. CS First may not work properly.
In this add on
you'll program a music playlist that users can change.
To start add music to your program.
Click the stage, select the sounds tab
and choose sound from library.
Select the music loops category, select the sound
then click the choose sound from library button again
and select another.
Select as many tracks as you'd like to play in your program.
Select each track and name every track as a number,
starting with one.
Next, start playing one of the tracks
when the flag is clicked.
Drag out a play sound until done block.
Above that snap a when flag clicked block.
Click the flag to test.
Great, but the music plays only once.
It should keep playing.
To make the music keep playing place a forever block
around the play sound until done block.
Test it out.
Awesome, the music repeats over and over.
Next, make the tracks change.
To change the music to the next track
you need to tell the program which track to play.
Create a music variable.
Select the menu and click make a variable.
Variables hold values, like numbers or words.
In this example, the variable will hold the track number.
Name the variable music and click okay.
Place the music block in the play sound until done block.
Now the program will play the sound
with the name that is in the music variable.
But right now, the music variable is zero
and there's no track named zero.
Drag out a set music to zero block
and change its value to one.
Place the block just after the when flag clicked block.
Click the flag to test it.
Great, it works!
Drag out a change music by one block
to try changing the track.
Cool, when the current track finishes playing
the track changes.
Snap the when right arrow key pressed block on top.
To test it, click the flag then press the right arrow key.
Cool, but the music variable will keep increasing forever
and you don't have that many music tracks.
To make the music variable stop increasing
after playing all the tracks,
add an if then block around the change music by one block.
Place a less than block inside the diamond shapes base.
On the left side place a music variable block.
On the right side
type the maximum number of tracks you have.
This example has four tracks.
Press the right arrow key multiple times.
Great, the music variable never goes beyond four.
Next program the left arrow to allow users to go backward
through the tracks.
Right click the when right arrow key pressed block stack
and select duplicate.
Change the dropdown to say left arrow key.
Change the green operator block to a greater than block.
On the left side, place a music variable block.
On the right side type one.
Type negative one in the change music by block.
Click the flag to test the whole thing.
Pressing the right and left arrow keys changes the track
but there's a bug in the program.
The music should change
as soon as the user presses the arrow key
not when the track finishes playing.
To fix the bug place a stop all sounds block
above the change music by one blocks.
Great, the music changes immediately
when the arrow keys are pressed.
Finally make sure your users know
that they can control the sound in the program.
Click the paint a new sprite button.
Use the text tool to type instructions like,
use the arrow keys to change the music.
Now everyone will know how to use the playlist you created.
Now it's your turn.
Add several music tracks to your program.
Program the sound to start when the flag is clicked.
All the user to change tracks by pressing the arrow keys
using change by and stop all sounds blocks.
Finally, paint a new sprite
to show off the music playlist feature.