2

I want to create 2 JMenuItem that can start and stop the background audio.

Here is my 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();        
            }
        }
    }
}

When I click the settingSubMenuItem1, it's work fine, audio is played.

But when I click the settingSubMenuItem2, there is errors and if click again settingSubMenuItem1, there will no more sound.

Here is the errors :

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

What is the error of my program?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Chin
  • 593
  • 4
  • 15
  • 36
  • 1
    See also this previous [question](http://stackoverflow.com/q/10666827/230513). – trashgod May 20 '12 at 02:48
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/). Hot-link to http://pscode.org/media/leftright.wav for a loopable sound. – Andrew Thompson May 20 '12 at 02:51
  • @trashgod, that's my question...... – Chin May 20 '12 at 02:59
  • Exactly; you should always cite your previous questions on the same topic: first, to avoid inadvertent closing as duplicate; second, to show answerers how your understanding has evolved. – trashgod May 20 '12 at 10:43

1 Answers1

2

This SSCCE is a 'null result' here, in that the audio restarts (tried at least 3 times) with no exceptions.

import java.net.URL;
import java.awt.event.*;
import javax.swing.*;
import javax.sound.sampled.*;

public class RestartableLoopSound {

    public static void main(String[] args) throws Exception {
        URL url = new URL(
            "http://pscode.org/media/leftright.wav");
        final Clip clip = AudioSystem.getClip();
        AudioInputStream ais = AudioSystem.
            getAudioInputStream( url );
        clip.open(ais);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                final JToggleButton b = new JToggleButton("Loop");
                ActionListener listener = new ActionListener() {
                    public void actionPerformed(ActionEvent ae) {
                        if (b.isSelected()) {
                            // loop continuously
                            clip.loop(Clip.LOOP_CONTINUOUSLY);
                        } else {
                            clip.stop();
                        }
                    }
                };
                b.addActionListener(listener);
                JOptionPane.showMessageDialog(null, b);
            }
        });
    }
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thanks again for your reply. But I think I won't use most of your code because I not yet know how to use `SwingUtilities`. However, I'll try to learn from your code. – Chin May 20 '12 at 03:15
  • *All* code that uses Swing components should start the GUI on the EDT. That can be achieved easily using one of 2 classes. `EventQueue` (AWT) or `SwingUtilities` (Swing). The latter simply makes calls back to the AWT class. Learn this stuff before making more GUIs - **it is important for stable, well-behaving GUIs.** – Andrew Thompson May 20 '12 at 03:20
  • 1
    +1 for [`invokeLater()`](http://download.oracle.com/javase/tutorial/uiswing/concurrency/initial.html). – trashgod May 20 '12 at 10:45
  • @trashgod Thanks for the edit! I cannot believe I looked at that 'magic number' so many times & it never jumped out & barked at me. I've edited the original source on the Java Sound info. page (from where that is adapted) and removed the (now quite pointless) single line comment that precedes. :) – Andrew Thompson May 20 '12 at 22:00
  • Excellent! Your [javasound](http://stackoverflow.com/tags/javasound/info) FAQ is a quite a _tour de force_. – trashgod May 20 '12 at 22:15
  • 1
    @trashgod Thanks! :) I feel there is no point to editing the WIKI unless you intend doing a good job of it. I use it as an exercise in distilling down my collected experience on a subject. – Andrew Thompson May 20 '12 at 22:18