Buzzing output

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(); // method calls Sound.killLoop() System.exit(0); } }); } } 
+4
source share
1 answer

In the Java API , they say that the AudioInputStream class has a ".close ()" method that "closes this audio input stream and frees up any system resources associated with the stream." Perhaps this is something you can try.

+1
source

Source: https://habr.com/ru/post/1492633/


All Articles