playSound

Category:UI Controls

Plays the MP3 sound file from the specified URL.

Today's apps play sounds to make them more engaging. You can add sounds to your apps that are triggered by events on UI elements, or based on turtle movements, or just based on other app code. There are two ways to fill in the url string for the parameter.

1. Copy the URL of a sound on the web. In most browsers you can simply right-click (ctrl+click on a Mac) on a sound file and you'll see a menu with a few option. One will be to copy the URL of the sound. Note: We have listed some existing audio files that you can use in your app below.

2. Upload your own sound file to App Lab. You can upload sound files saved on your computer to your app in App Lab.

  • Click the pulldown arrow in the image URL field and then click "Choose..."

  • Then click the "Upload File" button the in the window.

  • Then choose the file from your computer by navigating to it

  • Once its uploaded click "Choose" next to it. This will insert the name of the file into the URL field. Because you have uploaded it, it doesn't need to be an HTTP reference.

Examples

Example: Don't Go Too Far

Beep whenever the turtle moves to a position outside a rectangle at the center of the screen.

// Beep whenever the turtle moves to a position outside a rectangle at the center of the screen.
var count = 1;
while ((count <= 3)) {
  turnTo(randomNumber(1, 360));
  moveForward(randomNumber(25, 50));
  if (getX() < 100 || getX() > 220 || getY() < 165 || getY() > 285) {
    playSound("beep-01a.mp3");
    count = count+1;
  }
}

Animal Piano

Play an animal sound when each animal image is clicked.

// Play an animal sound when each animal image is clicked. 
image("dog", "http://animalia-life.com/data_images/dog/dog7.jpg");
setPosition("dog", 50, 100, 100, 100);
image("cat", "http://animalia-life.com/data_images/cat/cat1.jpg");
setPosition("cat", 150, 100, 100, 100);
image("pig", "http://animalia-life.com/data_images/pig/pig1.jpg");
setPosition("pig", 50, 200, 100, 100);
image("owl", "http://animalia-life.com/data_images/owl/owl1.jpg");
setPosition("owl", 150, 200, 100, 100);
onEvent("dog", "click", function(event) {
  playSound("http://static1.grsites.com/archive/sounds/animals/animals079.mp3");
});
onEvent("cat", "click", function(event) {
  playSound("http://static1.grsites.com/archive/sounds/animals/animals021.mp3");
});
onEvent("pig", "click", function(event) {
  playSound("http://static1.grsites.com/archive/sounds/animals/animals025.mp3");
});
onEvent("owl", "click", function(event) {
  playSound("http://static1.grsites.com/archive/sounds/animals/animals074.mp3");
});

// Play a goal sound.
playSound("https://studio.code.org/blockly/media/skins/studio/1_goal.mp3");

Syntax

playSound(url)

Parameters

Name Type Required? Description
url string The source URL (or filename for an uploaded file) of the MP3 sound file to be played.

Tips

  • The sound URL requires the full http:// prefix.

Found a bug in the documentation? Let us know at documentation@code.org