Playing Sounds

To add the playback of music and sound effects to your applications, you need to learn how to use the sound code blocks shown in Figure 11.1. These code blocks provide everything you need to play or stop the playback of MP3 and wave files in your Scratch applications.

Figure 11.1. These code blocks control audio file playback.


Note

A wave file is a type of file designed for storing an audio bit stream on personal computers. Wave files have a .wav file extension. An MP3 file is an audio file that utilizes advanced compression technology while retaining high audio quality.


The first two code blocks shown in Figure 11.1 let you play any MP3 or wave file that you add to your Scratch project. The third code block lets you stop the playback of all of the audio files belonging to a sprite. In order to play an audio file, you must first add it to a sprite or to the stage, which you can do by selecting the stage or a sprite from the sprite list, clicking on the Sounds tab location at the top of the scripts area, then clicking on the Import button. Once the file is imported, you can play the audio file using a script belonging to the stage or sprite, as demonstrated here.

In Figure 11.2, an audio file named meow is played when the green flag button is pressed. In order to play the audio file, you must select it from the code block’s drop-down list. The drop-down list is automatically populated by Scratch with all the audio files that have been added to the sprite to which the script belongs.

Figure 11.2. The meow audio file is played when the green flag button is clicked.


The sound code block used in the previous script allows the script to which it has been added to continue running. If the script containing the sound block has additional code left to be executed, the playback of the sound will be cut short when the script continues executing. This was not a problem in the previous example because the sound block was the last code block in the script.

For situations where you want to pause script execution to allow time for the entire audio file to finish playing, you have two choices. First, you can add a control block to the script immediately following the sound block that pauses script execution for a specified number of seconds (the number of seconds needed to play the audio file). Better yet, you can use the second code block shown in Figure 11.1 as demonstrated in the following script:

The sound code block used in this example plays an audio file that has been previously added to your Scratch application, pausing script execution until the audio file has finished playing. Once playback is complete, the rest of the script is permitted to finish its execution.

Tip

If you want to add the repeated playback of background music or sound effects to an application, create a script specifically for this purpose. This keeps the programming logic needed to play the audio file separate from other scripts and eliminates the need to pause other scripts’ execution to support audio playback.


Depending on what your applications are designed to do, there may be times when you want to stop the playback of audio files belonging to a sprite or the stage. This can be achieved using the third code block shown in Figure 11.1, as demonstrated in the following example:

Here, the playback of any audio files belonging to the sprite is immediately halted when the spacebar is pressed.

Note

In addition to playing any of the audio files supplied with Scratch, you can import external audio files, both MP3 and wave, into any sprite. If your computer has a microphone, you can record your own audio files by selecting a sprite or the stage, clicking on the Sounds tab located at the top of the scripts area, and then clicking on the Record button. This opens the Sound Recorder program shown in Figure 11.3. To record a custom sound, just click on the red Record button, and when you are done, click on OK. Once done, your new audio file will be displayed on the Sounds tab immediately available to your application.

Figure 11.3. Scratch makes it easy to record your own custom audio files.



..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.251.70