package de.miaurizius.jgame2d.core; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import javax.sound.sampled.Clip; import java.io.File; import java.net.MalformedURLException; import java.net.URL; import java.util.logging.Level; public class Sound { Clip clip; URL[] soundURL = new URL[30]; public Sound() { try { soundURL[0] = new File("assets/sounds/BlueBoyAdventure.wav").toURI().toURL(); soundURL[1] = new File("assets/sounds/coin.wav").toURI().toURL(); soundURL[2] = new File("assets/sounds/powerup.wav").toURI().toURL(); soundURL[3] = new File("assets/sounds/unlock.wav").toURI().toURL(); soundURL[4] = new File("assets/sounds/fanfare.wav").toURI().toURL(); } catch(MalformedURLException e) { Boot.logger.log(Level.SEVERE, e.getMessage()); } } public void setFile(int i) { try { AudioInputStream ais = AudioSystem.getAudioInputStream(soundURL[i]); clip = AudioSystem.getClip(); clip.open(ais); } catch (Exception e) { Boot.logger.log(Level.SEVERE, "Could not load Sound File: " + soundURL[i], e); } } public void play() { clip.start(); } public void loop() { clip.loop(Clip.LOOP_CONTINUOUSLY); } public void stop() { clip.stop(); } }