简体   繁体   中英

Java - Sound plays only once per sound, then will not play again

If I use equals() to compare strings, it plays the sound once, and then crashes. 如果我使用equals()比较字符串,它会播放一次声音,然后崩溃。 Does anyone see why the loop might be going out of bounds?

I tried inserting !myString.isEmpty and now it is no longer crashing, but it still only plays each sound once and will not play again after the first iteration. 我尝试插入!myString.isEmpty,现在它不再崩溃,但它仍然只播放每个声音一次,并且在第一次迭代后将不再播放。

We are trying to get a sound to play for a toy when the length of a button press is equal to a certain amount. Printing wise, the responses are as expected, (at least for short button presses) and it will print out that the array[1] spot is equal to "S", however for some reason this doesn't trigger our if function that should cause a sound to play when array[1] is equal to "S". Any ideas?

It is also, for some reason, not reading "L" or long button presses. It's driving us crazy we've been messing with it for like three hours! What do you smart guys think?

Java code from processing:

 import processing.serial.*; //accepts serial input
import ddf.minim.*; //imports minim sound library
Minim minim;
String [] array;

Serial arduinoPort; //reads serial from arduino
AudioPlayer au_player1, au_player2, au_player3; //initializes sound files, au_player(n)      (n=number of wav files)

void setup() {
  minim = new Minim(this);
  au_player1 = minim.loadFile("comply_not.wav");
  au_player2 = minim.loadFile("go_away.wav");
  String portName = Serial.list()[2];
  arduinoPort = new Serial(this, portName, 9600);
}


void draw() {
  while (arduinoPort.available () > 0) {
    String myString = arduinoPort.readStringUntil('\n');


    if ((myString != null)&&(!myString.isEmpty)) {
//      println(myString);
      printArray(array);
      array = split(myString, ",");
      if (array[1].equals("S")) {
         au_player1.play();
      }
       else if (array[1].equals("L")) {
        au_player2.play();
      }
      delay(100);
    }
  } 
}

void stop() {
  minim.stop();
  super.stop();
}

Arduino Code:

Essentially, this just reads in the button presses and then is sent over to processing which is all in Java so if you don't know the arduino stuff that's not a big deal. I think it's something to do with the Java.

#define BUTTON_PIN        2  // Button

#define LONGPRESS_LEN    25  // Min nr of loops for a long press
#define DELAY            100  // Delay per loop in ms
    Serial.print("No");
    Serial.print(",");
    Serial.print("No");
    Serial.print(",");
    Serial.println("No");
    break;
  case EV_SHORTPRESS:
    Serial.print(",");
    Serial.print("S");
    Serial.print(",");
    Serial.println("No");
    break;
  case EV_LONGPRESS:
    Serial.print(",");
    Serial.print("L");
    Serial.print(",");
    Serial.println("No");

Figured it out -

For some reason the sound needs to be rewinded!! (which to me is strange since it isn't a vhs hahaha but ok!)

Basically we added

soundclip.rewind();

for each sound clip and now it's working.

Thanks for your help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM