I have a simple Clip method, to play back small wav sounds. Yeah i found the method clip.loop() right now, but anyway i cannot understand why my method isnt working.
Heres the code:
public void playSound(final String sound, final boolean loop, final int repeatTime){
try {
sounds.get(sound).open();
}
catch(Exception e) {
System.out.println(e);
}
new Thread(new Runnable() {
public void run() {
int repeatCount = 1;
Clip actualClip = sounds.get(sound);
actualClip.setFramePosition(0);
actualClip.start();
while(true){
System.out.println("repeat c: " + repeatCount + ", frames left: " + (actualClip.getFrameLength() - actualClip.getFramePosition()));
if(actualClip.getFramePosition() >= actualClip.getFrameLength()){
actualClip.stop();
actualClip.setFramePosition(0);
actualClip.start();
repeatCount++;
}
if(!loop && repeatCount > repeatTime){
break;
}
}
actualClip.stop();
}
}).start();
}
its String sound, becaus im getting it out of an hashmap. When i call it with
playSound(key, false, 10);
The sound repeats itself 10 times. But when i call it again, (i do it with an button), its playing just 4 - 8 times, so the loop isnt working right. What im making wrong?
Thank you!
if(!loop || repeatCount > repeatTime)
should be OR rather then AND. for
loop instead of while
. open
ning the same clip over and over. To do that, you must first get a new AudioInputStream
to be open
ed by this Clip
.
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.