How would I make the sound play? It doesn't work!

Code:

import processing.sound.*; //import sound file from data folder
SoundFile file;

void setup() {
  size(800, 800);
}

void draw() {

  background(255);    
  drawFace(790, 750, 600, 600, 100, 673, 200, 245);    
  checkNose(120);    
  checkMouseNose();  
  checkMouseLeftEye();
  checkMouseRightEye();
  checkMouseMouth();
  checkEyes(250, 550, 200, 150);
}


void drawFace(int circSize, int circXSize, int mouthY, int mouthXSize, int mouthYSize, int tongueY, int tongueXSize, int tongueYSize) {
  noFill();
  ellipse(width/2, height/2, circXSize, circSize);

  if (checkMouseMouth()) {
    fill(0);
    ellipse(width/2, mouthY, mouthXSize, mouthYSize - 100);
  } else {
    noFill();
    ellipse(width/2, mouthY, mouthXSize, mouthYSize);
  }

  fill(231, 158, 169);
  ellipse(width/2, tongueY, tongueXSize, tongueYSize);
}

void checkEyes(int eyeX, int eyeX2, int eyeY, int eyeSize) {

  noFill();
  ellipse(eyeX, eyeY, eyeSize, eyeSize);
  ellipse(eyeX2, eyeY, eyeSize, eyeSize);

  ellipse(map(mouseX, 10, 500, 210, 260), map(mouseY, 0, 500, 160, 215), 40, 40);
  ellipse(map(mouseX, 0, 500, 210, 260), map(mouseY, 0, 500, 160, 215), 20, 20);
  ellipse(map(mouseX, 0, 500, 510, 560), map(mouseY, 0, 500, 160, 215), 40, 40);
  ellipse(map(mouseX, 0, 500, 510, 560), map(mouseY, 0, 500, 160, 215), 20, 20);

  if (checkMouseLeftEye()) {
    fill(255, 0, 255);
    ellipse(eyeX, eyeY, eyeSize, eyeSize);
  } else {
    noFill();
    ellipse(eyeX, eyeY, eyeSize, eyeSize);
  }

  if (checkMouseRightEye()) {
    fill(255, 0, 255);
    ellipse(eyeX2, eyeY, eyeSize, eyeSize);
  } else {
    noFill();
    ellipse(eyeX2, eyeY, eyeSize, eyeSize);
  }
}

void checkNose(int circSize) {

  if (checkMouseNose()) {
    fill(255, 0, 0);
    ellipse(width/2, height/2, circSize, circSize);
  } else {
    noFill();
    ellipse(width/2, height/2, circSize, circSize);
  }
}

boolean checkMouseNose() {

  if (mouseX < 460 && mouseX > 340 && mouseY < 460 && mouseY > 340 && mousePressed) {
    // Load a soundfile from the /data folder of the sketch and play it back
    file = new SoundFile(this, "horn.mp3");
    file.play();
    return true;
  }
  return false;
}

boolean checkMouseLeftEye() {

  if (mouseX < 325 && mouseX > 175 && mouseY < 325 && mouseY > 175 && mousePressed) {
    // Load a soundfile from the /data folder of the sketch and play it back
    file = new SoundFile(this, "ouch.mp3");
    file.play();
    return true;
  }
  return false;
}

boolean checkMouseRightEye() {

  if (mouseX < 625 && mouseX > 475 && mouseY < 325 && mouseY > 175 && mousePressed) {
    // Load a soundfile from the /data folder of the sketch and play it back
    file = new SoundFile(this, "ouch.mp3");
    file.play();
    return true;
  }
  return false;
}

boolean checkMouseMouth() {

  if (mouseX < 700 && mouseX > 100 && mouseY < 650 && mouseY > 550 && mousePressed) {
    // Load a soundfile from the /data folder of the sketch and play it back
    file = new SoundFile(this, "mouth.mp3");
    file.play();
    return true;
  }
  return false;
}

Answers

  • Check this code. Something to keep in mind is to load your resources in setup, including sound files.

    Kf

    import processing.sound.*; //import sound file from data folder
    
    final int NOSOUND=-1;
    final int HORN=0;
    final int OUCH=1;
    final int MOUTH=2;
    
    int n=3;  
    SoundFile[] file;
    int current=NOSOUND;
    
    void setup() {
      size(800, 800);
    
      file=new SoundFile[n] ;
      file[HORN]=new SoundFile(this, "ouch.mp3");
      file[OUCH]=new SoundFile(this, "ouch.mp3");
      file[MOUTH]=new SoundFile(this, "ouch.mp3");
    }
    
    void draw() {
    
    
      int temporal=current;
      current=NOSOUND;
    
      background(255);    
      drawFace(790, 750, 600, 600, 100, 673, 200, 245);    
      checkNose(120);    
      checkMouseNose();  
      checkMouseLeftEye();
      checkMouseRightEye();
      checkMouseMouth();
      checkEyes(250, 550, 200, 150);
    
    
      if (temporal!=current) {
        println("C="+current);
        silenceAll();
    
        if (current!=NOSOUND) {
          file[current].play();
        }
      }
    
    
    }
    
    void silenceAll() {
      for (SoundFile ss : file) {
        ss.stop();
      }
      current=NOSOUND;
    }
    
    
    void drawFace(int circSize, int circXSize, int mouthY, int mouthXSize, int mouthYSize, int tongueY, int tongueXSize, int tongueYSize) {
      noFill();
      ellipse(width/2, height/2, circXSize, circSize);
    
      if (checkMouseMouth()) {
        fill(0);
        ellipse(width/2, mouthY, mouthXSize, mouthYSize - 100);
      } else {
        noFill();
        ellipse(width/2, mouthY, mouthXSize, mouthYSize);
      }
    
      fill(231, 158, 169);
      ellipse(width/2, tongueY, tongueXSize, tongueYSize);
    }
    
    void checkEyes(int eyeX, int eyeX2, int eyeY, int eyeSize) {
    
      noFill();
      ellipse(eyeX, eyeY, eyeSize, eyeSize);
      ellipse(eyeX2, eyeY, eyeSize, eyeSize);
    
      ellipse(map(mouseX, 10, 500, 210, 260), map(mouseY, 0, 500, 160, 215), 40, 40);
      ellipse(map(mouseX, 0, 500, 210, 260), map(mouseY, 0, 500, 160, 215), 20, 20);
      ellipse(map(mouseX, 0, 500, 510, 560), map(mouseY, 0, 500, 160, 215), 40, 40);
      ellipse(map(mouseX, 0, 500, 510, 560), map(mouseY, 0, 500, 160, 215), 20, 20);
    
      if (checkMouseLeftEye()) {
        fill(255, 0, 255);
        ellipse(eyeX, eyeY, eyeSize, eyeSize);
      } else {
        noFill();
        ellipse(eyeX, eyeY, eyeSize, eyeSize);
      }
    
      if (checkMouseRightEye()) {
        fill(255, 0, 255);
        ellipse(eyeX2, eyeY, eyeSize, eyeSize);
      } else {
        noFill();
        ellipse(eyeX2, eyeY, eyeSize, eyeSize);
      }
    }
    
    void checkNose(int circSize) {
    
      if (checkMouseNose()) {
        fill(255, 0, 0);
        ellipse(width/2, height/2, circSize, circSize);
      } else {
        noFill();
        ellipse(width/2, height/2, circSize, circSize);
      }
    }
    
    boolean checkMouseNose() {
    
      if (mouseX < 460 && mouseX > 340 && mouseY < 460 && mouseY > 340 && mousePressed) {
        // Load a soundfile from the /data folder of the sketch and play it back
    
        current=HORN;
        return true;
      }
      return false;
    }
    
    boolean checkMouseLeftEye() {
    
      if (mouseX < 325 && mouseX > 175 && mouseY < 325 && mouseY > 175 && mousePressed) {
        // Load a soundfile from the /data folder of the sketch and play it back
        current=OUCH;
        return true;
      }
      return false;
    }
    
    boolean checkMouseRightEye() {
    
      if (mouseX < 625 && mouseX > 475 && mouseY < 325 && mouseY > 175 && mousePressed) {
        // Load a soundfile from the /data folder of the sketch and play it back
        current=OUCH;
        return true;
      }
      return false;
    }
    
    boolean checkMouseMouth() {
    
      if (mouseX < 700 && mouseX > 100 && mouseY < 650 && mouseY > 550 && mousePressed) {
        // Load a soundfile from the /data folder of the sketch and play it back
        current=MOUTH;
        return true;
      }
      return false;
    }
    
  • The sound doesn't play though! @kfrajer

  • Double check the filename, make sure the file is in the data directory.

    Are there errors on the console?

  • Do the following change:

      if (temporal!=current) {
        println("C="+current);
        //silenceAll();     ///   <-----------
    

    Kf

  • edited May 19

    It then gives an error "NullPointerException" for:

             if (current!=NOSOUND) {
                  file[current].play();
                }
    
  • Send me a private message with your code. You need to make sure the files exist in your data folder and that the definitions in setup are correct. You only have three files, right?

    Kf

  • Answer ✓

    I ran your code and it worked. I didn't have any issues.

    Try adding some println() statements to figure which file is the problem. For example:

      if (current!=NOSOUND) {
         println("Current file:"+current);
         file[current].play();
       }
    

    Kf

  • There is only one file, used 3 times.

    And if that's null it points to the file not loading, which means it's a bad file or not in the right place, both of which should give warnings on the console.

  • thx, I just named the files wrong

Sign In or Register to comment.