How do I get the sound files in my sketch to loop? The sound only plays once

edited December 2017 in Library Questions

Hi forums, I'm needing a little guidance with a simple thing here. Not sure if it's a code problem or some of the problems I've been reading about sound files not looping. I have the most up to date sound library and I'm on Processing 3.3.6. The sound files are mp3's. I'm also unsure of the if statements and where I currently have the loop function entered. I basically want this sketch to display the letter that is pressed, and play the sound file associated to it - but loop that sound until another key is pressed, playing another sound file.

Pasting the code here:

import processing.sound.*;
import punktiert.math.Vec;
import punktiert.physics.*;

int x;
int y;
String typedWord = "";
//String[] sounds = {"a"," b", "c"}; 
SoundFile soundA,soundB,soundC,soundD,soundE,soundF,soundG,soundH,
soundI,soundJ,soundK,soundL,soundM,soundN,soundO,soundP,soundQ,soundR,soundS,soundT;

void setup() {
  size(1080 ,720);
  soundA = new SoundFile (this, "a.mp3");
  soundB = new SoundFile (this, "b.mp3");
  soundC = new SoundFile (this, "c.mp3");

}

void draw() {
  textSize(100);
  background (0);
  fill (random (255),random (255),random (255));
  text(typedWord, 250, height/2);

}

void keyPressed() {
  //println(key);
  if (key != 65535) {
    if (keyCode == BACKSPACE) {
  if (typedWord.length() > 0) {
    typedWord = typedWord.substring(0, typedWord.length()-1); //erases the last character
  }
    }
    else {
  typedWord += key; //add the key to the word
  }
  }
     }


void keyReleased() {
     if ((key == 'a') || (key == 'A')) {
       soundA.loop();
     }
     if ((key == 'b') || (key == 'B')) {
       soundB.loop();
     }
      if ((key == 'c') || (key == 'C')) {
       soundC.loop();
      }
}

Answers

Sign In or Register to comment.