We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, I've got an audio sample to trigger when a key is hit on the keyboard. But if I write the code in this way, it continuously plays during the time I hit the key but I want to trigger the sample only once, doesn't matter how long I hit the key. It seems the thing I need is a stop function but I couldn't figure out how I should write. Thanks.
Sample s;
import ddf.minim.*;
Minim soundengine;
void setup() {
size(900, 600);
s = new Sample();
soundengine=new Minim(this);
}
void draw() {
background(30);
if (keyPressed) {
kickvisual();
}
if (keyPressed) {
s.play();
}
void kickvisual() {
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
cellCollection[i][j].run();
}
}
}
}
class Sample {
AudioSample q1;
Sample () {
q1=soundengine.loadSample("kik.wav", 128);
}
void play() {
keyPressed();
}
void keyPressed()
{
if ( key == 'Q' ) q1.trigger();
}
}
Answers
Check out isPlaying():
http://code.compartmental.net/minim/javadoc/ddf/minim/AudioPlayer.html#isPlaying().html
Should give you a starting point to prevent retriggers and shut offs. I also benefitted from the noLoop() function on a similar project.
Thank you for your advice, but the mistake I did there was putting the if(keyPressed) under the draw function. I created another function called keyPressed and it works great!