89 lines
2.7 KiB
Java
89 lines
2.7 KiB
Java
import javazoom.jl.decoder.JavaLayerException;
|
|
import javazoom.jl.player.Player;
|
|
|
|
import java.awt.List;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileNotFoundException;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.Iterator;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* Der SoundHandler dient zum Anlegen und Abspielen von Sounds
|
|
* @author Lucas Bronson, Luca Conte, Ole Wachtel, Joshua Kuklok
|
|
*/
|
|
public class SoundHandler {
|
|
|
|
private static boolean soundOn = true;
|
|
|
|
private static ArrayList<Thread> runningThreads = new ArrayList<Thread>();
|
|
|
|
// Wenn fehler beim erstellen von .jar mit sound hier gucken
|
|
private static HashMap<String, String> sounds = new HashMap<String, String>(Map.of(
|
|
"miss", "./Sound/water-drip.mp3",
|
|
"hit", "./Sound/hit.mp3",
|
|
"destroyed", "./Sound/hit.mp3",
|
|
"plop", "./Sound/plop.mp3",
|
|
"loose", "./Sound/loosescreenWAH.mp3"
|
|
));
|
|
|
|
/**
|
|
* Erstellt neuen Thread, um ausgewählten Sound abzuspielen
|
|
* @param soundName Name der Audiodatei, welche abgespielt werden soll
|
|
* @author Ole Wachtel, Luca Conte
|
|
*/
|
|
public static void playSound(String soundName) {
|
|
if (soundOn) {
|
|
Thread thread = new Thread(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
try {
|
|
Player player = new Player(new FileInputStream(sounds.get(soundName)));
|
|
player.play();
|
|
} catch (JavaLayerException | FileNotFoundException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
});
|
|
thread.start();
|
|
runningThreads.add(thread);
|
|
}
|
|
Iterator<Thread> i = runningThreads.iterator();
|
|
while (i.hasNext()) {
|
|
Thread oldThread = i.next();
|
|
if (!oldThread.isAlive()) {
|
|
|
|
try {
|
|
oldThread.join();
|
|
i.remove();
|
|
System.out.println("cleared thread");
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* TODO funktion beschreiben (potentiell nicht benötigte Funktion?)
|
|
* @param soundName
|
|
* @param path
|
|
* @author Ole Wachtel
|
|
*/
|
|
static void add(String soundName, String path){
|
|
sounds.put(soundName, path);
|
|
}
|
|
|
|
/**
|
|
* TODO funktion beschreiben (potentiell nicht benötigte Funktion?)
|
|
* @param sound
|
|
* @author Ole Wachtel
|
|
*/
|
|
static void setSoundOn(boolean sound){
|
|
soundOn= sound;
|
|
}
|
|
|
|
}
|