Skip to content

Instantly share code, notes, and snippets.

@tmshv
Created December 8, 2015 17:57
Show Gist options
  • Save tmshv/0f470a8d14eeab8fba62 to your computer and use it in GitHub Desktop.
Save tmshv/0f470a8d14eeab8fba62 to your computer and use it in GitHub Desktop.
Processing Sound Toggle
import processing.sound.*;
class Sound {
SoundFile file;
boolean playing;
Sound(PApplet app, String path){
file = new SoundFile(app, path);
playing = true;
}
void loop(){
if(file != null) file.loop();
}
void play(){
if(file != null){
playing = true;
file.play();
}
}
void stop(){
if(file != null){
playing = false;
file.stop();
}
}
void toggle(){
if(playing) stop();
else play();
}
}
Sound sound1;
Sound sound2;
void setup() {
sound1 = new Sound(this, "sound1.mp3");
sound2 = new Sound(this, "sound2.mp3");
sound1.loop();
sound2.loop();
sound2.stop();
}
void mousePressed(){
sound1.toggle();
sound2.toggle();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment