Astronomy Hour of Code

Activity 15: Playing an Audio File from the Server


Now we are going to learn how to add sounds to our program. Audio can be very useful to provide the user feedback about what the computer is doing, particularly to non-sighted users. To begin, we are just going to create an Audio object, load a sound and then play it. Before we can start working with Audio objects though, we need to tell the compiler where to find the object in a library. In Quorum, the library is located at Libraries.Sound.Audio and we reference it with the use command. We always need to put the use commands at the top of the program before we try to reference any objects in the library. So, the first statement in our program should look like this:

use Libraries.Sound.Audio

Next, we will create an Audio object by declaring it, similar to how we created a variable with a type and a name. Let's create an Audio object called clicksound like this:

Audio clickSound

Before we can do anything with the clickSound object, we need to load an audio file into it. We do that with the Load command. We call the Load command by putting a ":" after the name of the object and typing Load with the path of the file in parenthesis. There is a sound called click.wav on the webserver located at the path "media/astro/" so to load it into the clickSound object, we can use this command:

clickSound:Load( "media/astro/click.wav")

Now all we have to do is tell the sound to play. To do that, we can call the Play action of our Audio object. If we call an action on an object it always needs to have parenthesis after the name of the action and the action name is capitalized, like this:


When you are done, click the green Run Program button and listen to the sound.

Exploration Challenge (Optional):

For this Exploration Challenge, put the Play() statement in a repeat loop to play the audio more than once. If you want to make your program more interactive, get input from the user to ask how many times the sound should play.

Next Tutorial

In the next tutorial, we will discuss Astronomy Hour of Code | Activity 0, which describes Accessible astronomy themed Hour of Code.