We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi I've made this small game, when a letter is pressed its displayed on screen as an image and a sound plays. When I run the code and press a letter it shows the letter and plays the sound, but when I press the letter again it displays the letter but doesn't play the sound. I've tried using .rewind(); and what happens after that it first time I press a letter it plays the sound and displays the letter, and if I press the same letter again it displays the letter and when i release the key it plays the sound :S.
import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;
Minim minim;
AudioPlayer ha;
AudioPlayer sh;
AudioPlayer no;
AudioPlayer ra;
AudioPlayer ba;
AudioPlayer lh;
PImage[] letters = new PImage[6];
void setup() {
size(400, 400);
background(0);
minim = new Minim(this);
ha = minim.loadFile("0.mp3");
sh = minim.loadFile("1.mp3");
no = minim.loadFile("2.mp3");
ra = minim.loadFile("3.mp3");
ba = minim.loadFile("4.mp3");
lh = minim.loadFile("5.mp3");
imageMode(CENTER);
for (int i = 0; i<letters.length; i++) {
letters[i] = loadImage(+i+".jpg");
}
}
void draw() {
background(0);
if ((keyPressed == true)&&(key == 'h')) {
image(letters[0], width/2, height/2, 200, 200);
ha.play();
}
if ((keyPressed == true)&&(key == 's')) {
image(letters[1], width/2, height/2, 200, 200);
sh.play();
}
if ((keyPressed == true)&&(key == 'n')) {
image(letters[2], width/2, height/2, 200, 200);
no.play();
}
if ((keyPressed == true)&&(key == 'r')) {
image(letters[3], width/2, height/2, 200, 200);
ra.play();
}
if ((keyPressed == true)&&(key == 'b')) {
image(letters[4], width/2, height/2, 200, 200);
ba.play();
}
if ((keyPressed == true)&&(key == 'l')) {
image(letters[5], width/2, height/2, 200, 200);
lh.play();
}
}
Answers
I would suggest trying a small modification in your draw method. Assign the variable ha right before you call play like this:
Run your program and see if thist improves your case.
Kf
@kfrajer no it didn't improve it. it keeps loading in the loop. i tried putting an else {ha.rewind();} after the if but then it keeps rewinding as it loops.
never load files in draw. it's an expensive operation and is likely to cause delays.
you have 6 blocks all starting with
you could simplify that by moving
if (keyPressed)
so it's called once around the whole lot.however, i'd use keyPressed(), the method, instead. see the reference for the differences and why you'd choose one over the other.
you have an array for images, why not an array for the audio players as well?
none of this fixes your problem though 8)
@koogs i got confused putting the audio files into an array, could you help me thanks
This is untested code. Give it a try.
Kf
I don't think you need minim at all in your code. This works for me
Kf