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

**URL:** https://discourse.processing.org/t/i-cant-make-sound-effect-on-processing-quiz/28848
**Category:** Coding Questions
**Tags:** homework
**Created:** [March 25, 2021, 9:11pm UTC](https://discourse.processing.org/t/i-cant-make-sound-effect-on-processing-quiz/28848 "2021-03-25T21:11:18Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![Ellen](https://avatars.discourse-cdn.com/v4/letter/e/dfb087/32.png) [@Ellen](https://discourse.processing.org/u/Ellen)
#### Post date: [March 25, 2021, 9:11pm UTC](https://discourse.processing.org/t/i-cant-make-sound-effect-on-processing-quiz/28848/1 "2021-03-25T21:11:18Z")

</div>

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;  
}  
//

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 25, 2021, 9:23pm UTC](https://discourse.processing.org/t/i-cant-make-sound-effect-on-processing-quiz/28848/2 "2021-03-25T21:23:51Z")

</div>

> [@Ellen](#):
>
> file.play();

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.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 25, 2021, 9:39pm UTC](https://discourse.processing.org/t/i-cant-make-sound-effect-on-processing-quiz/28848/3 "2021-03-25T21:39:05Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Ellen](https://avatars.discourse-cdn.com/v4/letter/e/dfb087/32.png) [@Ellen](https://discourse.processing.org/u/Ellen)
#### Post date: [March 25, 2021, 9:41pm UTC](https://discourse.processing.org/t/i-cant-make-sound-effect-on-processing-quiz/28848/4 "2021-03-25T21:41:39Z")

</div>

> [@Ellen](#):
>
> play

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

and

void goOn() {

I think??

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 25, 2021, 9:43pm UTC](https://discourse.processing.org/t/i-cant-make-sound-effect-on-processing-quiz/28848/5 "2021-03-25T21:43:50Z")

</div>

In my understanding:

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

```

AND

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

```

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

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 25, 2021, 9:52pm UTC](https://discourse.processing.org/t/i-cant-make-sound-effect-on-processing-quiz/28848/6 "2021-03-25T21:52:46Z")

</div>

> [@Ellen](#):
>
> void goOn() {

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

---

<div class="post-metadata">

### Author: ![Ellen](https://avatars.discourse-cdn.com/v4/letter/e/dfb087/32.png) [@Ellen](https://discourse.processing.org/u/Ellen)
#### Post date: [March 25, 2021, 9:54pm UTC](https://discourse.processing.org/t/i-cant-make-sound-effect-on-processing-quiz/28848/7 "2021-03-25T21:54:00Z")

</div>

```auto
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

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 25, 2021, 9:54pm UTC](https://discourse.processing.org/t/i-cant-make-sound-effect-on-processing-quiz/28848/8 "2021-03-25T21:54:57Z")

</div>

**That’s perfect!**

Does it work?

Does it do what you want?

---

<div class="post-metadata">

### Author: ![Ellen](https://avatars.discourse-cdn.com/v4/letter/e/dfb087/32.png) [@Ellen](https://discourse.processing.org/u/Ellen)
#### Post date: [March 25, 2021, 9:57pm UTC](https://discourse.processing.org/t/i-cant-make-sound-effect-on-processing-quiz/28848/9 "2021-03-25T21:57:07Z")

</div>

> [@Ellen](#):
>
> ```auto
> 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();
> 
> ```

No, It doesn’t.

```auto

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

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 25, 2021, 9:59pm UTC](https://discourse.processing.org/t/i-cant-make-sound-effect-on-processing-quiz/28848/10 "2021-03-25T21:59:16Z")

</div>

Do you receive an error message like **file not found**?

**Question**

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

```

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

---

<div class="post-metadata">

### Author: ![Ellen](https://avatars.discourse-cdn.com/v4/letter/e/dfb087/32.png) [@Ellen](https://discourse.processing.org/u/Ellen)
#### Post date: [March 25, 2021, 10:00pm UTC](https://discourse.processing.org/t/i-cant-make-sound-effect-on-processing-quiz/28848/11 "2021-03-25T22:00:28Z")

</div>

> [@Chrisir](#):
>
> `file.play(); `

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.”

---

<div class="post-metadata">

### Author: ![Ellen](https://avatars.discourse-cdn.com/v4/letter/e/dfb087/32.png) [@Ellen](https://discourse.processing.org/u/Ellen)
#### Post date: [March 25, 2021, 10:01pm UTC](https://discourse.processing.org/t/i-cant-make-sound-effect-on-processing-quiz/28848/12 "2021-03-25T22:01:59Z")

</div>

Oh my god, I think I got it

shoud I download the library, right???

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 25, 2021, 10:03pm UTC](https://discourse.processing.org/t/i-cant-make-sound-effect-on-processing-quiz/28848/13 "2021-03-25T22:03:24Z")

</div>

yes, you have to download / install

there is a **Manager** in processing

Menu Sketch | Import Library | Add library…

search sound, select and install

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 25, 2021, 10:07pm UTC](https://discourse.processing.org/t/i-cant-make-sound-effect-on-processing-quiz/28848/14 "2021-03-25T22:07:42Z")

</div>

> [@Ellen](#):
>
> ` totalCorrect++; file.play();`

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

Then I think this line becomes two lines :

```auto
       totalCorrect++; 
       file.play();

```

---

<div class="post-metadata">

### Author: ![Ellen](https://avatars.discourse-cdn.com/v4/letter/e/dfb087/32.png) [@Ellen](https://discourse.processing.org/u/Ellen)
#### Post date: [March 25, 2021, 10:08pm UTC](https://discourse.processing.org/t/i-cant-make-sound-effect-on-processing-quiz/28848/15 "2021-03-25T22:08:27Z")

</div>

YES It works!!!

Thank you so so so much!

---

<div class="post-metadata">

### Author: ![Ellen](https://avatars.discourse-cdn.com/v4/letter/e/dfb087/32.png) [@Ellen](https://discourse.processing.org/u/Ellen)
#### Post date: [March 25, 2021, 10:51pm UTC](https://discourse.processing.org/t/i-cant-make-sound-effect-on-processing-quiz/28848/16 "2021-03-25T22:51:09Z")

</div>

Hi Chrisir!  
Can I ask one more thing?

I wanna add a sound for random quiz,

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

```

Is it wrong?

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 26, 2021, 9:15am UTC](https://discourse.processing.org/t/i-cant-make-sound-effect-on-processing-quiz/28848/17 "2021-03-26T09:15:20Z")

</div>

> [@Ellen](#):
>
> add a sound for random quiz

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?
