add Sound in to Thread

This commit is contained in:
Ole Wachtel 2024-10-29 14:34:57 +01:00
parent 810ffe7c5f
commit 521f6a578f
2 changed files with 16 additions and 10 deletions

View File

@ -7,9 +7,9 @@ public class HalloSchiffeVersenken {
System.out.println("sound");
SoundHandler.playSound("hit");
Thread.sleep(3000);
System.out.println("sound");
SoundHandler.playSound("hit");
Thread.sleep(10000);
SoundHandler.setSoundOn(false);
System.out.println("sound off");
SoundHandler.playSound("hit");

View File

@ -16,15 +16,21 @@ public class SoundHandler {
public static void playSound(String soundName) {
if (soundOn) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Player player = new Player(new FileInputStream(sounds.get(soundName)));
player.play();
} catch (JavaLayerException | FileNotFoundException e) {
System.out.println("dslkfsfnsldfnlsnfsdnölscls");
e.printStackTrace();
}
}
}).start();
}
}
static void add(String soundName, String path){
sounds.put(soundName, path);