Note that there are some explanatory texts on larger screens.

plurals
  1. POError playing AudioInputStream
    primarykey
    data
    text
    <p>I want to create 2 <code>JMenuItem</code> that can start and stop the background audio.</p> <p>Here is my code : </p> <pre><code>public class MainClass extends JFrame { private AudioInputStream audioInputStream; private Clip clip; public MainClass(String title) { try { audioInputStream = AudioSystem.getAudioInputStream(new File("Background.wav")); clip = AudioSystem.getClip(); clip.loop(Clip.LOOP_CONTINUOUSLY); clip.open(audioInputStream); } catch(Exception e) { System.out.println("Error with playing sound."); e.printStackTrace(); } } public void startSound() { clip3.start(); settingSubMenuItem1.setEnabled(false); settingSubMenuItem2.setEnabled(true); } public void stopSound() { clip3.stop(); settingSubMenuItem1.setEnabled(true); settingSubMenuItem2.setEnabled(false); } private class MenuItemListener implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource() == settingSubMenuItem1) { startSound(); } if(e.getSource() == settingSubMenuItem2) { stopSound(); } } } } </code></pre> <p>When I click the <code>settingSubMenuItem1</code>, it's work fine, audio is played.</p> <p>But when I click the <code>settingSubMenuItem2</code>, there is errors and if click again <code>settingSubMenuItem1</code>, there will no more sound.</p> <p>Here is the errors : </p> <pre><code>Error with playing sound. java.lang.IllegalStateException: Clip is already open with format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian and frame lengh of 7658 </code></pre> <p>What is the error of my program?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload