# Sound in Processing

**URL:** https://discourse.processing.org/t/sound-in-processing/4728
**Category:** Libraries
**Created:** [October 21, 2018, 10:25pm UTC](https://discourse.processing.org/t/sound-in-processing/4728 "2018-10-21T22:25:18Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Lore](https://avatars.discourse-cdn.com/v4/letter/l/9dc877/32.png) [@Lore](https://discourse.processing.org/u/Lore)
#### Post date: [October 21, 2018, 10:25pm UTC](https://discourse.processing.org/t/sound-in-processing/4728/1 "2018-10-21T22:25:18Z")

</div>

Hello,  
How do you stop a song that is playing? I know how to play a song in processing, but I was wondering how to make the current song stop playing.

Here is my code,

```auto
//Make movie variables
//Load every movie
//If todraw is a variable load that movie
import processing.video.*; 
int up = 80;
int right = 7;
int toDraw = 1;
// Step 1. Declare a Movie object.
Movie movie; 

import processing.sound.*;
SoundFile file;

void setup()
{  
  file = new SoundFile(this, "music1.aiff");
  movie = new Movie(this, "movie1.mov");  
  file.loop();
  noCursor();
  size(636, 476);  
  println("IMPORTANT: READ BELOW");
  println("Right and left increase and decrease the brightness of the flashlight.");
  println("Up and down increase and decrease the size of the flashlight.");
  println("Control removes the flashlight, alt makes a black hole, and shift resets everything.");
  println("And don't make anything a negative value.");
}
// Step 4. Read new frames from the movie.
void movieEvent(Movie movie) 
{  
  movie.read();
}
// Step 5. Display movie.
void draw() 
{
  movie.loop();
  loadPixels();
  movie.loadPixels();
  for (int x = 0; x < movie.width; x++) 
  {    
    for (int y = 0; y < movie.height; y++) 
    {      
      // Calculate the 1D location from a 2D grid
      int loc = x + y * movie.width;      
      // Get the red, green, blue values from a pixel      
      float r = red (movie.pixels[loc]);      
      float g = green(movie.pixels[loc]);      
      float b = blue (movie.pixels[loc]);      
      // Calculate an amount to change brightness based on proximity to the mouse      
      float d = dist(x, y, mouseX, mouseY);      
      float adjustbrightness = map(d, 0, up, right, 0);      
      r *= adjustbrightness;      
      g *= adjustbrightness;      
      b *= adjustbrightness;      
      // Constrain RGB to make sure they are within 0-255 color range      
      r = constrain(r, 0, 255);      
      g = constrain(g, 0, 255);      
      b = constrain(b, 0, 255);      
      // Make a new color and set pixel in the window      
      color c = color(r, g, b);      
      pixels[loc] = c;
    }
  }  
  updatePixels();
  
}
void keyPressed()
{
  if (key == CODED)
  {
    if (keyCode == UP)
    {
      up=up+5;
    }
    if (keyCode == DOWN)
    {
      up=up-5;
    }
    if (keyCode == RIGHT)
    {
      right=right+1;
    }
    if (keyCode == LEFT)
    {
      right=right-1;
    }
    if (keyCode == CONTROL)
    {
      right=2;
      up=1900;
      file.play(1.0, 1.0);
    }
    if (keyCode == SHIFT)
    {
      right=7;
      up=80;
    }
    if (keyCode == ALT)
    {
      right=-10;
      up=70;
    }
  }
}

```

You can see that under `if (keyCode == CONTROL)` Everytime the control button is pressed, it plays that specific soundfile. However, the soundfile never stops playing so there are many versions of it playing at the same time. How do I prevent this?

---

<div class="post-metadata">

### Author: ![PHuzzeyMusic](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@PHuzzeyMusic](https://discourse.processing.org/u/PHuzzeyMusic)
#### Post date: [October 22, 2018, 12:56am UTC](https://discourse.processing.org/t/sound-in-processing/4728/2 "2018-10-22T00:56:42Z")

</div>

This fix certainly isn’t efficient but it works

```auto
 file.stop();
 file = new SoundFile(this, "music1.aiff");
 file.play();
}

```

---

<div class="post-metadata">

### Author: ![Lore](https://avatars.discourse-cdn.com/v4/letter/l/9dc877/32.png) [@Lore](https://discourse.processing.org/u/Lore)
#### Post date: [October 22, 2018, 7:28am UTC](https://discourse.processing.org/t/sound-in-processing/4728/3 "2018-10-22T07:28:39Z")

</div>

@PHuzzeyMusic

Does `file.play();` loop the soundfile or only play it once? Would I use `file.loop();` instead to loop the soundfile?

---

<div class="post-metadata">

### Author: ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)
#### Post date: [October 23, 2018, 6:34pm UTC](https://discourse.processing.org/t/sound-in-processing/4728/4 "2018-10-23T18:34:36Z")

</div>

If i understand it correctly, then what he means, is When you press Control it stops the old sound, creates it again and Plays it, until the next Time you press Control. The only Problem is, that the sound would not Overlap, But also keep on going until you press again, and Start Over… You Would maybe want to add a method that Takes the length of the sound file and stops it after that many seconds
