[ControlP5] How to use a button
in
Contributed Library Questions
•
4 months ago
Hi, I have a question about sound buttion of this code.
I want it to start sound if I click the button and the sound will repeat until I click the button again (If I click the button again, then it has to stop)
I've tried this code if I click the button the sound keeps coming out even though I pressed it again.
How do i have to change?
my code:
- import controlP5.*;
import ddf.minim.*; - //Minim declaration
Minim minim;
AudioPlayer player; - ControlP5 cp5;
- int myColor = color(0);
void setup() {
size(1024,768);
cp5 = new ControlP5(this);
minim = new Minim(this);
// replace the default controlP5 button with an image.
// button.setImages(defaultImage, rolloverImage, pressedImage);
// use button.updateSize() to adjust the size of the button and
// resize to the dimensions of the defaultImage
cp5.addButton("buttonA")
.setPosition(175,575)
.setImages(loadImage("Arrow-Left.png"), loadImage("Arrow-Right.png"), loadImage("Refresh.png"))
.updateSize();
cp5.addButton("buttonB")
.setPosition(275,575)
.setImages(loadImage("Arrow-Left.png"), loadImage("Arrow-Right.png"), loadImage("Refresh.png"))
.updateSize();
cp5.addButton("buttonC")
.setPosition(375,575)
.setImages(loadImage("Arrow-Left.png"), loadImage("Arrow-Right.png"), loadImage("Refresh.png"))
.updateSize();
cp5.addButton("buttonD")
.setPosition(475,575)
.setImages(loadImage("Arrow-Left.png"), loadImage("Arrow-Right.png"), loadImage("Refresh.png"))
.updateSize();
cp5.addButton("buttonE")
.setPosition(575,575)
.setImages(loadImage("Arrow-Left.png"), loadImage("Arrow-Right.png"), loadImage("Refresh.png"))
.updateSize();
cp5.addButton("buttonF")
.setPosition(675,575)
.setImages(loadImage("Arrow-Left.png"), loadImage("Arrow-Right.png"), loadImage("Refresh.png"))
.updateSize();
cp5.addButton("buttonG")
.setPosition(775,575)
.setImages(loadImage("Arrow-Left.png"), loadImage("Arrow-Right.png"), loadImage("Refresh.png"))
.updateSize();
}- void draw() {
background(myColor);
} - public void controlEvent(ControlEvent theEvent) {
println(theEvent.getController().getName());
String buttonName = theEvent.getController().getName();
//load the file depends on button
if(buttonName == "buttonA"){
player = minim.loadFile("1vol.wav");
}else if(buttonName == "buttonB"){
player = minim.loadFile("2vol.wav");
}
//play
player.play();
} - // function buttonA will receive changes from
// controller with name buttonA
public void buttonA(int theValue) {
println("a button event from buttonA: "+theValue);
myColor = color(20);
} - public void buttonB(int theValue) {
println("a button event from buttonB: "+theValue);
myColor = color(50);
} - public void buttonC(int theValue) {
println("a button event from buttonC: "+theValue);
myColor = color(80);
} - public void buttonD(int theValue) {
println("a button event from buttonD: "+theValue);
myColor = color(120);
} - public void buttonE(int theValue) {
println("a button event from buttonE: "+theValue);
myColor = color(160);
} - public void buttonF(int theValue) {
println("a button event from buttonF: "+theValue);
myColor = color(210);
} - public void buttonG(int theValue) {
println("a button event from buttonG: "+theValue);
myColor = color(250);
}
1