.rewind() making sound replay nonstop

Hey all,

I'm trying to play a sound when someone scores.

I have it working for score1 - so the first time CPU plays sound1 but then it stops and doesn't work again.
I watched a tutorial and it said you need .rewind() to reset the sound so it will play again I added it but it sounds like a chainsaw. It keeps on looping.

import processing.serial.*; // Import the Processing Serial Library for communicating with arduino
import ddf.minim.*;

Minim minim;

AudioPlayer sound1, sound2, sound3, sound4;

Serial myPort;              
int CPU_SCORE;
int PLAYER_SCORE;

 void setup()
{
  size(500, 500);
  println(Serial.list()); // Prints the list of serial available devices (Arduino should be on top of the list)
  myPort = new Serial(this, Serial.list()[1], 9600); // Open a new port and connect with Arduino at 9600 baud

  minim = new Minim(this);

  sound1 = minim.loadFile("roundLose.wav");
  sound2 = minim.loadFile("gameLose.wav");
  sound3 = minim.loadFile("gameWin.mp3");
  sound4 = minim.loadFile("roundWin.wav");
}
  

void draw()
{
  
  if(CPU_SCORE == 1) {
    sound1.play();
    sound1.rewind();
  }
  
  if(CPU_SCORE == 2) {
    sound1.play();
    sound1.rewind();
  }
    
  if(CPU_SCORE == 3) {
    sound1.play();
    sound1.rewind();
  }

  else if(CPU_SCORE == 4) {
    sound1.play();
    sound1.rewind();
  }
  
  if(CPU_SCORE == 5) {
    sound1.play();
    sound1.rewind();
  }
  
  if(CPU_SCORE == 6) {
    sound1.play();
    sound1.rewind();
  }
    
  if(CPU_SCORE == 7) {
    sound1.play();
    sound1.rewind();
  }

  if(CPU_SCORE == 8) {
    sound2.play();
  }
  
  print(CPU_SCORE);
  println(PLAYER_SCORE);
  
 }
  


void serialEvent(Serial myPort) // Is called everytime there is new data to read
{
  if (myPort.available() > 1)
  {
    String completeString = myPort.readStringUntil(10); // Read the Serial port until there is a linefeed/carriage return
    if (completeString != null) // If there is valid data insode the String
    {
      trim(completeString); // Remove whitespace characters at the beginning and end of the string
      String seperateValues[] = split(completeString, ","); // Split the string everytime a delimiter is received
      CPU_SCORE = int(seperateValues[0]);
      PLAYER_SCORE = int(seperateValues[1]);
    }
  }
}

(deleted)

there you go sorry

Ardi2743:
there you go sorry

What about the rest of it ?

Please read How to use this forum and follow the advice re posting code using code tags

update sorry again! :frowning:

The code now in your original post is not Arduino code. It looks very much like it is Processing code.

Is your problem with the Arduino or with Processing ?

yeah it's with processing I didn't even think about it being an issue, sorry I'm new to all this.

(deleted)

spycatcher2k:
This is not the processing forum, this is the ARDUINO forum! :slight_smile:

But you need to play the sound when the score changes, have a flag that is set when the score changes, and only play the sound when the flag is set AND the score is at a value, then reset the plag in the play calling function.

Psudo code:

bool flag = false;
check if i need to add to the score;
if i do
{
score++;
flag = true;
}

if(score == 2 && flag == true)
{Playsound;
rewind;
flag = false;
}

Thank you! Yeah that's my bad xD