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]);
}
}
}