Make audio replay on keypress

edited December 2013 in Questions about Code

Ok, so I'm pretty new to this, but for my code I want to make images & audio play on key press. I can get this to work, but when you press a different key and then press back to the original key - the image plays, but not the audio. Any suggestions to make audio re-play each time that key is pressed? Any help would be greatly appreciated!

Working on a mac, processing 2.0, images are 1440 x 856

Here's my code:

import ddf.minim.*;

Minim minim;
AudioPlayer player1; 
AudioPlayer player2;
AudioPlayer player3;


boolean square1 = false;
boolean square2 = false;
boolean square3 = false;

PImage img;
PImage img1;
PImage img2;
PImage img3;


void setup() {
  size (1440, 856);
  img = loadImage("d.jpg");
  background(img);
  minim = new Minim(this);
  player1 = minim.loadFile("1.mp3");
  player2 = minim.loadFile("2.mp3");
  player3 = minim.loadFile("3.mp3");

}

void draw(){


  if(square1 == false && keyCode == LEFT ){
    img1 = loadImage("a.jpg");
    image(img1,0,0);
    player1.play();
    square1=true;
    square2=false;
    square3=false;

  }


  if(square2 == false && keyCode == RIGHT ){
    img2 = loadImage("b.jpg");
    image(img2,0,0);
    player2.play();
    square1=false;
    square2=true;
    square3=false;
  }

    if (keyPressed) {
    if (key == 'w' || key == 'W'){
    img3 = loadImage("c.jpg");
    image(img3,0,0);
    player3.play();
    square1=false;
    square2=false;
    square3=false;
    square4=true;


  }
    }


}

void stop()
{

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

Answers

Sign In or Register to comment.