add SoundHandler to Play sounds and Mute global

This commit is contained in:
Ole Wachtel 2024-10-28 16:23:48 +01:00
parent b8a450c7dc
commit 07b839fef5
6 changed files with 84 additions and 1 deletions

View File

@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="jlayer-1.0.3">
<CLASSES>
<root url="jar://$USER_HOME$/Downloads/jlayer-master/jlayer-master/target/jlayer-1.0.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

View File

@ -7,5 +7,6 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="jlayer-1.0.3" level="project" />
</component>
</module>

View File

@ -1,8 +1,19 @@
public class HalloSchiffeVersenken {
public static void main(String[] args){
public static void main(String[] args) throws InterruptedException {
System.out.println("HelloSchiffeVersenekn");
System.out.println("sound");
SoundHandler.playSound("hit");
Thread.sleep(3000);
System.out.println("sound");
SoundHandler.playSound("hit");
SoundHandler.setSoundOn(false);
System.out.println("sound off");
SoundHandler.playSound("hit");
}
}

View File

@ -0,0 +1,37 @@
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class SoundHandler {
private static boolean soundOn = true;
private static HashMap<String, String> sounds = new HashMap<String, String>(Map.of(
"hit", "~/../../Sound/water-drip.mp3"
));
public static void playSound(String soundName) {
if (soundOn) {
try {
Player player = new Player(new FileInputStream(sounds.get(soundName)));
player.play();
} catch (JavaLayerException | FileNotFoundException e) {
System.out.println("dslkfsfnsldfnlsnfsdnölscls");
e.printStackTrace();
}
}
}
static void add(String soundName, String path){
sounds.put(soundName, path);
}
static void setSoundOn(boolean sound){
soundOn= sound;
}
}

View File

@ -0,0 +1,25 @@
public class Sounds {
private String soundName;
private String path;
public Sounds(String soundName, String path){
setSoundName(soundName);
setPath(path);
}
public String getSoundName() {
return soundName;
}
public void setSoundName(String soundName) {
this.soundName = soundName;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}

BIN
Sound/water-drip.mp3 Normal file

Binary file not shown.