SoundManager.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package view.sound;
  2. import javax.sound.sampled.*;
  3. import java.io.IOException;
  4. import java.net.URL;
  5. import java.util.EnumMap;
  6. public class SoundManager{
  7. private static SoundManager instance;
  8. private EnumMap<SOUNDS, Clip> soundMap;
  9. public void stopLoopSound(SOUNDS sound) {
  10. soundMap.get(sound).stop();
  11. }
  12. public void setVolume(Clip clip, float percent) {
  13. if (clip.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
  14. FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
  15. float min = gainControl.getMinimum(); // usually -80.0f
  16. float max = gainControl.getMaximum(); // usually 6.0f or 0.0f
  17. // Convert percent (0–100) to decibel scale
  18. float range = max - min;
  19. float gain = (range * percent / 100f) + min;
  20. gainControl.setValue(gain);
  21. } else {
  22. System.err.println("Volume control not supported for this clip.");
  23. }
  24. }
  25. public void setVolume(float volume) {
  26. soundMap.forEach((sound, clip) -> {
  27. setVolume(clip, volume);
  28. });
  29. }
  30. public enum SOUNDS {
  31. CLICK,
  32. MOVING_BUSH,
  33. VILLAGER
  34. }
  35. private SoundManager() {
  36. soundMap = new EnumMap<>(SOUNDS.class);
  37. }
  38. public void loadAllSounds(){
  39. SoundManager.getInstance().loadSound(SoundManager.SOUNDS.CLICK, "sound/click.wav");
  40. SoundManager.getInstance().loadSound(SoundManager.SOUNDS.VILLAGER, "sound/villager.wav");
  41. SoundManager.getInstance().loadSound(SoundManager.SOUNDS.MOVING_BUSH, "sound/moving_bush.wav");
  42. }
  43. public void loopSound(SOUNDS sound){
  44. Clip clip = soundMap.get(sound);
  45. if(clip != null){
  46. if(clip.isRunning()){
  47. clip.stop();
  48. }else{
  49. clip.loop(Clip.LOOP_CONTINUOUSLY);
  50. }
  51. }
  52. }
  53. public static SoundManager getInstance() {
  54. if (instance == null) {
  55. instance = new SoundManager();
  56. }
  57. return instance;
  58. }
  59. public void loadSound(SOUNDS sound, String resourcePath) {
  60. try {
  61. URL soundURL = getClass().getClassLoader().getResource(resourcePath);
  62. if (soundURL == null) {
  63. System.err.println("Resource not found: " + resourcePath);
  64. return;
  65. }
  66. AudioInputStream audioStream = AudioSystem.getAudioInputStream(soundURL);
  67. Clip clip = AudioSystem.getClip();
  68. clip.open(audioStream);
  69. soundMap.put(sound, clip);
  70. } catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) {
  71. System.err.println("Failed to load sound " + sound + ": " + e.getMessage());
  72. }
  73. }
  74. public void playSound(SOUNDS sound) {
  75. Clip clip = soundMap.get(sound);
  76. if (clip != null) {
  77. if (clip.isRunning()) {
  78. clip.stop();
  79. }
  80. clip.setFramePosition(0);
  81. clip.start();
  82. } else {
  83. System.err.println("Sound " + sound + " wurde nicht geladen.");
  84. }
  85. }
  86. public void unloadAllSounds() {
  87. for (Clip clip : soundMap.values()) {
  88. clip.close();
  89. }
  90. soundMap.clear();
  91. }
  92. }