stopSound()

Category:Game Lab

Stops playing the MP3 sound file from the specified URL, or stops all sounds if no URL is given.

When programming your games you may need to be able to control when to stop playing or looping a sound. You can stop sounds in your games using mouse or keyboard actions, or based on sprite collisions, or just based on other game code. There are two ways to fill in the url string for the optional parameter: A full URL of a sound or the file name of a sound you have uploaded to Game Lab.

Examples

Stop the Music

Stop the background sound when a sprite reaches the edge of the display area.

// Stop the background sound when a sprite reaches the edge of the display area.
playSound("https://audio.code.org/win3.mp3", true);
var sprite = createSprite(200, 200);
sprite.setSpeedAndDirection(randomNumber(-5, -5), randomNumber(-180, 180));
function draw() {
  background("white");
  drawSprites();
  if (sprite.x<0 || sprite.x>400 || sprite.y<0 || sprite.y>400 ) {
    stopSound("https://audio.code.org/win3.mp3");
  }
}

Syntax

stopSound(url)

Parameters

NameTypeRequired?Description
url String The source URL (or filename for an uploaded file) of the MP3 sound file to be stopped. If no parameter is specified all sounds are stopped.

Returns

No return value. Stops playing a sound only.

Tips

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

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