From 1337f286e5de1752455a3ccb97fe9af537cabf75 Mon Sep 17 00:00:00 2001
From: jg175 <jg175@hdm-stuttgart.de>
Date: Thu, 14 Dec 2023 13:22:09 +0100
Subject: [PATCH] Fix: Play sound repeatedly on demand

---
 .../hdm_stuttgart/battlearena/Main/Main.java  |  4 +-
 .../battlearena/Model/Sound/SoundManager.java | 51 ++++++++++++-------
 2 files changed, 36 insertions(+), 19 deletions(-)

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 17fd0cae..466f0a5f 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 a9235e11..b2519543 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:
-- 
GitLab