Unit 4 Loops, Arrays, Media
Learning Activity 4

Play sounds


Overview

In this learning activity students load and play different sound files in response to user actions. Pointers are given for further exploration of audio manipulation.

Suggested Duration

30 minutes

Instructions

1. Play a sound

The first step to playing a sound file is to upload it to a folder in the online editor, just like we did with our images in the previous LA. Files must be smaller than 5mb.

Like images and fonts, sound files must be loaded into a variable before we can use them. Once a sound file is loaded, we can call play().

2. Play four different sounds

In this example, we play different sound files when the user presses different keys. Press 'A' for a bleep, 'S' for a 'woof', 'D' for a bass note, 'F' for a guitar pluck. Before typing, remember to click on the sketch:

Sounds can also be looped (loop()), stopped (stop()), jump to a specific moment (jump), and manipulated in many creative ways. See more under the Sound category on the p5 examples page, and the p5.sound reference page.