diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Main/Main.java b/src/main/java/de/hdm_stuttgart/battlearena/Main/Main.java index 17fd0cae60bb277f16cd7d1acd9adfa4dd67bb8e..466f0a5fb534b1989daa40b817575dfdf6df4961 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Main/Main.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Main/Main.java @@ -32,7 +32,9 @@ public class Main extends Application { SoundManager soundManager = new SoundManager(); //soundManager.playSound(); soundManager.playSoundEffect(SFX.WALKING, true); - soundManager.playSoundEffect(SFX.WALKING, false); //TODO: Soundeffect should stop here, but it doesnot + + + //soundManager.playSoundEffect(SFX.WALKING, false); stage.show(); log.debug("Project started successfully!"); } diff --git a/src/main/java/de/hdm_stuttgart/battlearena/Model/Sound/SoundManager.java b/src/main/java/de/hdm_stuttgart/battlearena/Model/Sound/SoundManager.java index a9235e118995f1e4b6fcb2608bf7d11df445c8a7..b251954356a08c62d3e69c6669bb4b55f32ea6b4 100644 --- a/src/main/java/de/hdm_stuttgart/battlearena/Model/Sound/SoundManager.java +++ b/src/main/java/de/hdm_stuttgart/battlearena/Model/Sound/SoundManager.java @@ -27,7 +27,7 @@ public class SoundManager implements ISoundManager { private ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(5); - + private volatile boolean stopLoop = false; private List<Path> walkingSFXPaths; @@ -47,28 +47,43 @@ public class SoundManager implements ISoundManager { public void playSoundEffect(SFX effects, boolean playLoop) throws InterruptedException, MalformedURLException { switch (effects){ case WALKING: - Path path = Paths.get("src/main/resources/sound/sfx/steps"); + Path path = Paths.get("src/main/resources/sound/sfx/steps"); try { walkingSFXPaths = getFilePathsFromResources(path); } catch (IOException e) { throw new RuntimeException(e); } - while (playLoop) { - //Choose random Walk Sound - Random random = new Random(); - int min = 0; - int max = walkingSFXPaths.size() - 1; - int positiveRandomInRange = random.nextInt(max - min + 1) + min; - Path walkSound = walkingSFXPaths.get(positiveRandomInRange); - - //Play Clip - System.out.println(walkSound); - AudioClip audioClip = new AudioClip(walkSound.toUri().toURL().toExternalForm()); - audioClip.setVolume(1.0); - executor.execute(() -> audioClip.play()); - executor.awaitTermination(650, TimeUnit.MILLISECONDS); - - } + if (playLoop) { + stopLoop = false; // Reset flag for new loop + executor.execute(() -> { + while (!stopLoop) { // Continue playing until the flag is set to true + Random random = new Random(); + int min = 0; + int max = walkingSFXPaths.size() - 1; + int positiveRandomInRange = random.nextInt(max - min + 1) + min; + Path walkSound = walkingSFXPaths.get(positiveRandomInRange); + + System.out.println(walkSound); + AudioClip audioClip = null; + try { + audioClip = new AudioClip(walkSound.toUri().toURL().toExternalForm()); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } + audioClip.setVolume(1.0); + audioClip.play(); + + try { + Thread.sleep(650); // Wait before playing the next clip + } catch (InterruptedException e) { + // Handle interruption if needed + break; + } + } + }); + } else { + stopLoop = true; // Set flag to stop the loop + } break; case COLLISION: