I can't make sound effect on processing (Quiz)

I want to make a function for.
if I click the correct answer, that will make a sound

I tried it, but it isn’t working
I don’t know what is wrong… can anybody help me?

import processing.sound.*;
SoundFile file;

String[] Texts = {
“Question A”,
“Question B”,
“Question C”,
“Question D”,
“Question E”,
“Question F”,
“Question G”
};

String[] RightAnswer = {
“Yes”,
“No”,
“Yes”,
“No”,
“No”,
“Yes”,
“No”
};

int n = int (random(0, 6));

//String myText;
int totalWrong = 0;
int totalCorrect = 0;
//int question = 0;
//String correctAnswer;
//String WrongAnswer;
//int True= 0;
//int False= 0;

void setup() {
size(800, 400);
background(255);
smooth();
ellipseMode(CENTER);

file = new SoundFile(this, “bell.wav”);
file.play();
}

void draw() {
// draw() runs 60 times per second again and again

// we clear the screen to WHITE
background(255);

// we show stuff
noStroke();

//Button 1
fill(255, 20, 40);
if (button1())
fill(0, 255, 0);
ellipse(320, 100, 20, 20);
text(“YES”, 320+22, 100);

//Button 2
fill(255, 20, 40);
if (button2())
fill(0, 255, 0);
ellipse(420, 100, 20, 20);
text(“NO”, 420+22, 100);

fill(0);
rect(width/2, height/2, 300, 100);
textSize(20);
text(Texts[n], 10, 30);
text(RightAnswer[n], 10, 30+22); // for testing what happens!!!

fill(255);

fill(0);
text("", 0, 300, 800, 700);
//text(question, 20, 80);
text("Correct? ", 20+110+55, 80+30);
text(“Correct:”, 600, 40);
text(totalCorrect, 620, 80);
text(“Incorrect:”, 600, 140);
text(totalWrong, 620, 180);
}

void mousePressed() {
// CHECK Buttons
if (button1()) {
// button 1: Yes
println(“button1”);
if (RightAnswer[n].equals(“Yes”)) {
totalCorrect++;
} else {
totalWrong++;
}
goOn();
} else if (button2()) {
// button 2: No
println(“button2”);
if (RightAnswer[n].equals(“No”)) {
totalCorrect++;
} else {
totalWrong++;
}
goOn();
}//else if
}

void goOn() {
n = int (random(0, 6));
}

boolean button1() {
return
dist(mouseX, mouseY, 320, 100) < 70;
}

boolean button2() {
return
dist(mouseX, mouseY, 480, 100) < 70;
}
//

this line: can you hear the sound when the program starts??

this line belongs in the place in the code where the program detects that a correct answer has been given.

Do you know where this is? There are 2 places obviously.

you cannot expect that processing executes the sound play command at the right time when you place it at the beginning - instead remember that processing executes your Sketch line by line,

  • starting before setup(),

  • doing setup() once and

  • then draw() again and again.

  • When the mouse is pressed the function mousePressed gets executed.

So the play command must be in the section of the code where the correct answer has been given.

By the way, the text() command for the buttons uses Yes and No, maybe True and False is better

if (RightAnswer[n].equals(“Yes”)) {

and

void goOn() {

I think??

In my understanding:

if (RightAnswer[n].equals(“Yes”)) {
    totalCorrect++;

AND

if (RightAnswer[n].equals(“No”)) {
    totalCorrect++;

(because for some sentences TRUE is the correct answer, for others FALSE is the correct answer)

this is called also when the question was answered in a wrong way

void mousePressed() {
  // CHECK Buttons
  if (button1()) {
    // button 1: Yes
    println("button1");
    if (RightAnswer[n].equals("Yes")) {
      totalCorrect++; file.play();
    } else {
      totalWrong++;
    }
    goOn();
  } else if (button2()) {
    // button 2: No
    println("button2");
    if (RightAnswer[n].equals("No")) {
      totalCorrect++; file.play();
    } else {
      totalWrong++;
    }
    goOn();

not like this?
Sorry I’m so confused

That’s perfect!

Does it work?

Does it do what you want?

No, It doesn’t.


import processing.sound.*;
SoundFile file;

void setup() {
  size(800, 400);
  background(255);
  smooth();
  ellipseMode(CENTER);
  
  file = new SoundFile(this, "s.wav");
}

I think those are not working In that part

Do you receive an error message like file not found?

Question

void mousePressed() {
  file.play(); 
  // CHECK Buttons

Does this work when you click mouse, do you hear a sound?

that continue to show

“No library found for processing.sound
Libraries must be installed in a folder named ‘libraries’ inside the sketchbook folder (see the Preferences window).
The package “processing.sound” does not exist. You might be missing a library.”

Oh my god, I think I got it

shoud I download the library, right???

yes, you have to download / install

there is a Manager in processing

Menu Sketch | Import Library | Add library…

search sound, select and install

Remember to hit Ctrl-t in processing to get auto-format

Then I think this line becomes two lines :

       totalCorrect++; 
       file.play();

YES It works!!!

Thank you so so so much!

1 Like

Hi Chrisir!
Can I ask one more thing?

I wanna add a sound for random quiz,

if (Texts == [1]){
    sound1.play();
  }

Is it wrong?

Excuse me, do you mean when a new question is started?

What do you mean by random quiz?

a new question is started in function goOn(). There you should have sound1.play();. Maybe it’s tricky when the other sound (from the correct answer) is still playing?