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,
//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?