I am trying to create a game that uses sound effects. I have not done Java API before, so I could be wrong. However, the effects work great - my only problem is that I get a strange buzzing sound, maybe a second, when my program comes out.
Any idea how I can get rid of it? Right now, I'm trying to kill any playback sounds right before exiting using the killLoop () method, but that won't go anywhere.
I would be grateful for your help!
public class Sound { private AudioInputStream audio; private Clip clip; public Sound(String location) { try { audio = AudioSystem.getAudioInputStream(new File(location)); clip = AudioSystem.getClip(); clip.open(audio); } catch(UnsupportedAudioFileException uae) { System.out.println(uae); } catch(IOException ioe) { System.out.println(ioe); } catch(LineUnavailableException lua) { System.out.println(lua); } } public void play() { clip.setFramePosition(0); clip.start(); } public void loop() { clip.loop(clip.LOOP_CONTINUOUSLY); } public void killLoop() { clip.stop(); clip.close(); } } public class Athenaeum { public static void main(String[] args) throws IOException { final Game game = new Game(); GUI athenaeumGui = new GUI(game); athenaeumGui.setSize(GUI.FRAME_WIDTH, GUI.FRAME_HEIGHT); athenaeumGui.setTitle("Athenaeum"); athenaeumGui.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); athenaeumGui.setLocationRelativeTo(null); athenaeumGui.setMinimumSize(new Dimension(GUI.FRAME_WIDTH, GUI.FRAME_HEIGHT)); athenaeumGui.buildGui(); athenaeumGui.setVisible(true); athenaeumGui.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent we) { game.killAudio();
source share