Sound Code in void draw makes me problem

I want the sound to be played when the ball touches the player. At the moment I have the sound in void draw and that makes me problems because messages appear in the console and then the game blocks.
Can I somehow write it in void setup so that the sound plays when the player touches the ball?

void draw(){
  background(bg);
  rect(spieler_x, spieler_y, 20, 100);
  rect(ball_x, ball_y, 10 ,10);
  
  //Blur-Effekt
  for(int i = (blur_anzahl - 1); i > 0; i--){
    fill(255, 0, 0, 255/(i*2));
    positions_array[i][0] = positions_array[i-1][0];
    positions_array[i][1] = positions_array[i-1][1];
    rect(positions_array[i][0], positions_array[i][1], 10 ,10);
  }
  positions_array[0][0] = ball_x;
  positions_array[0][1] = ball_y;
  fill(200, 0, 0, 255);
 
  
  if(keyPressed){
    if(keyCode == DOWN){
      if(spieler_y < 650){
        spieler_y = spieler_y + 5;
      }
    }
    if(keyCode == UP){
      if(spieler_y > 50){  
        spieler_y = spieler_y - 5;
      }
    }
  }
  ball_x = ball_x + ball_geschwindigkeit_x;
  ball_y = ball_y + ball_geschwindigkeit_y;
  
  if(ball_x < 30){
    if(ball_y < (spieler_y + 55) && ball_y > (spieler_y - 55)){
      file = new SoundFile(this, "plop.mp3");    //Sound
      file.play();
      ball_geschwindigkeit_x = (-ball_geschwindigkeit_x) + 1;
      ball_geschwindigkeit_y = ball_geschwindigkeit_y - (spieler_y - ball_y) * 0.1;
      runde = runde + 1;
    }else{
       file = new SoundFile(this, "game_over.mp3");    //Sound
       file.play();

Hi TheKillerHD,

Please avoid posting picture of your code.

Instead copy/paste your code here, select it and hit the code format button </>

Thanks :slight_smile:

Okay I changed it now. But can you help me with this problem?

You want to load your sound in the setup() function: that’s made for it.

What you are currently doing right now is loading your song over and over every time you want to play it.

Instead create a global variable, load it in setup and play it when needed.

I don’t understand how I can do this. Can you give me pls an example how I can do it? I have reuploaded my post with the command because I have forgot that I have two sounds.

You can open the following example in processing:

ProcSoundEx

It will give you an idea on how it works.

Then you can replace the example code with the following:

import processing.sound.*;

SoundFile soundfile;

void setup() {
  size(640, 360);
  background(255);

  //Load a soundfile
  soundfile = new SoundFile(this, "vibraphon.aiff");
}

void draw() {
}


void mousePressed() {
  if (soundfile.isPlaying() == 1) {
    soundfile.stop();
  } else {
    soundfile.play();
  }
}

What it will do is play and stop the sound alternatively every time you hit the mouse.

And how can I do this with two sounds?

Just declare 2 variables like you would do for any other kind of variables:

import processing.sound.*;

SoundFile soundfile1;
SoundFile soundfile2;

void setup() {
  ...
  soundfile1 = new SoundFile(this, "file1.aiff");
  soundfile2 = new SoundFile(this, "file2.aiff");
}

Now the game don’t block, but I became the same message in the console and I don’t know how I can’t put the soundfile1.play(); and the soundfile2.play(); in the void draw.

void setup(){
  positions_array = new float [blur_anzahl][2];
  spieler_x = 20;
  spieler_y = 353;
  ball_x = 450;
  ball_y = 350;
  ball_geschwindigkeit_x = - 4;
  ball_geschwindigkeit_y = 0;
  runde = 0;
  size (900, 700);
  rectMode(CENTER);
  bg = loadImage("pong_bg.png");    //Hintergrundbild
  noStroke();
  schrift = loadFont("burbank30.vlw");
  textFont(schrift);
  
  //Sound
  soundfile1 = new SoundFile(this, "plop.mp3");
  soundfile2 = new SoundFile(this, "game_over.mp3");
}

void draw(){
  background(bg);
  rect(spieler_x, spieler_y, 20, 100);
  rect(ball_x, ball_y, 10 ,10);
  
  //Blur-Effekt
  for(int i = (blur_anzahl - 1); i > 0; i--){
    fill(255, 0, 0, 255/(i*2));
    positions_array[i][0] = positions_array[i-1][0];
    positions_array[i][1] = positions_array[i-1][1];
    rect(positions_array[i][0], positions_array[i][1], 10 ,10);
  }
  positions_array[0][0] = ball_x;
  positions_array[0][1] = ball_y;
  fill(200, 0, 0, 255);
 
  
  if(keyPressed){
    if(keyCode == DOWN){
      if(spieler_y < 650){
        spieler_y = spieler_y + 5;
      }
    }
    if(keyCode == UP){
      if(spieler_y > 50){  
        spieler_y = spieler_y - 5;
      }
    }
  }
  ball_x = ball_x + ball_geschwindigkeit_x;
  ball_y = ball_y + ball_geschwindigkeit_y;
  
  //This is when then ball touches the player
  if(ball_x < 30){
    if(ball_y < (spieler_y + 55) && ball_y > (spieler_y - 55)){
      soundfile1.play();    //Sound
      ball_geschwindigkeit_x = (-ball_geschwindigkeit_x) + 1;
      ball_geschwindigkeit_y = ball_geschwindigkeit_y - (spieler_y - ball_y) * 0.1;
      runde = runde + 1;
    }else{
       soundfile2.play();    //Sound
       ball_x = 450;
       ball_y = 350;
       ball_geschwindigkeit_x = - 4;
       ball_geschwindigkeit_y = 0;
       runde = 0;
       spieler_y = 353;

Can you tell us what is the error message?

This is the message: Sep 14, 2018 9:50:38 PM com.jsyn.devices.javasound.JavaSoundAudioDevice
INFORMATION: JSyn: default output latency set to 80 msec for Windows 10
Sep 14, 2018 9:50:38 PM com.jsyn.engine.SynthesisEngine start
INFORMATION: Pure Java JSyn from www.softsynth.com, rate = 44100, RT, V16.8.0 (build 463, 2017-10-16)
Sep 14, 2018 9:50:50 PM com.jsyn.engine.SynthesisEngine$EngineThread run
INFORMATION: JSyn synthesis thread in finally code.

Is there still a problem with the Processing Sound library? If so, I would recommend using the old minim library. Should work well :smiley:

EnhancedLoop7

Okay I try, let’s hope it works.

Minim doesn’t work because I haven’t the old processing verison. I have the 3.4 processing version and that’s the problem why I can’t use the minim library.

The message in the console comes now only if I click on the playbutton.

Maybe @kevinstadler can help, he is updating the sound library.

@TheKillerHD

This is not an error message, just information about the sound library loading, you can ignore it (I’ll try to suppress the message in later versions of the library).

Is there actually anything that’s not working about the sound playback, except for the fact that it prints this message?

@jb4x people repeatedly loading the same sound file in draw() shouldn’t actually be a problem, the new Sound library first checks if it has already loaded a file from the same path and simply reuses the same one from memory!

Hoo, I didn’t know about that one! Thanks for clarifying :slight_smile:

Still, I prefer to load everything once for clarity in the code.

What is strange is that doing so apparently changed the behavior of his sketch:

1 Like

No, everything works and I’m very grateful that you helped me so well with the problem.

1 Like