|
|
@@ -0,0 +1,89 @@
|
|
|
+package view.sound;
|
|
|
+
|
|
|
+import javax.sound.sampled.*;
|
|
|
+import java.io.IOException;
|
|
|
+import java.net.URL;
|
|
|
+import java.util.EnumMap;
|
|
|
+
|
|
|
+public class SoundManager{
|
|
|
+ private static SoundManager instance;
|
|
|
+ private EnumMap<SOUNDS, Clip> soundMap;
|
|
|
+
|
|
|
+ public void stopLoopSound(SOUNDS sound) {
|
|
|
+ soundMap.get(sound).stop();
|
|
|
+ }
|
|
|
+
|
|
|
+ public enum SOUNDS {
|
|
|
+ CLICK,
|
|
|
+ MOVING_BUSH,
|
|
|
+ VILLAGER
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private SoundManager() {
|
|
|
+ soundMap = new EnumMap<>(SOUNDS.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void loadAllSounds(){
|
|
|
+ SoundManager.getInstance().loadSound(SoundManager.SOUNDS.CLICK, "sound/click.wav");
|
|
|
+ SoundManager.getInstance().loadSound(SoundManager.SOUNDS.VILLAGER, "sound/villager.wav");
|
|
|
+ SoundManager.getInstance().loadSound(SoundManager.SOUNDS.MOVING_BUSH, "sound/moving_bush.wav");
|
|
|
+ }
|
|
|
+
|
|
|
+ public void loopSound(SOUNDS sound){
|
|
|
+ Clip clip = soundMap.get(sound);
|
|
|
+ if(clip != null){
|
|
|
+ if(clip.isRunning()){
|
|
|
+ clip.stop();
|
|
|
+ }else{
|
|
|
+ clip.loop(Clip.LOOP_CONTINUOUSLY);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static SoundManager getInstance() {
|
|
|
+ if (instance == null) {
|
|
|
+ instance = new SoundManager();
|
|
|
+ }
|
|
|
+ return instance;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void loadSound(SOUNDS sound, String resourcePath) {
|
|
|
+ try {
|
|
|
+ URL soundURL = getClass().getClassLoader().getResource(resourcePath);
|
|
|
+ if (soundURL == null) {
|
|
|
+ System.err.println("Resource not found: " + resourcePath);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ AudioInputStream audioStream = AudioSystem.getAudioInputStream(soundURL);
|
|
|
+ Clip clip = AudioSystem.getClip();
|
|
|
+ clip.open(audioStream);
|
|
|
+ soundMap.put(sound, clip);
|
|
|
+ } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
|
|
|
+ System.err.println("Failed to load sound " + sound + ": " + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void playSound(SOUNDS sound) {
|
|
|
+
|
|
|
+ Clip clip = soundMap.get(sound);
|
|
|
+ if (clip != null) {
|
|
|
+ if (clip.isRunning()) {
|
|
|
+ clip.stop();
|
|
|
+ }
|
|
|
+ clip.setFramePosition(0);
|
|
|
+ clip.start();
|
|
|
+ } else {
|
|
|
+ System.err.println("Sound " + sound + " wurde nicht geladen.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void unloadAllSounds() {
|
|
|
+ for (Clip clip : soundMap.values()) {
|
|
|
+ clip.close();
|
|
|
+ }
|
|
|
+ soundMap.clear();
|
|
|
+ }
|
|
|
+}
|