Could not play a sound in swing

194 views Asked by At

I am trying to play a .wav file everytime I press a button.

I've put the resource in the workspace, together with my project. Is that wrong?

I get this error message:

javax.sound.sampled.AudioInputStream@341bdd4c /home/fred/workspace/Projekt/src/GUI/sound/gangnam.wav java.lang.IllegalArgumentException: Invalid format

Here is the code:

public static void main (String [] args) {
    JFrame customBoardFrame = new JFrame("Custom Size.");
    customBoardFrame.getContentPane().add(new customSize());    
    customBoardFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    customBoardFrame.setSize(450, 310);
    customBoardFrame.setVisible(true);
    customBoardFrame.setResizable(false);
}

public class customSize extends JPanel implements ActionListener {

    JButton playButton = new JButton(" >> PLAY <<");
    JButton stopButton = new JButton(" >> STOP <<");
    JButton pauseButton = new JButton(" >> PAUSE <<");

    public customSize(){

        setLayout(null);

        playButton.setBounds(12, 173, 116, 40);
        add(playButton);

        playButton.addActionListener(this);
        pauseButton.addActionListener(this);
        stopButton.addActionListener(this);

        stopButton.setBounds(140, 173, 117, 40);
        add(stopButton);

        pauseButton.setBounds(269, 173, 140, 40);
        add(pauseButton);

    }

    public void actionPerformed(ActionEvent e){

        if (e.getSource().equals(playButton)) {

            try {
                String soundName = "gangnam.wav";    
                AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(soundName).getAbsoluteFile());
                System.out.println(audioInputStream);
                System.out.println(new File (soundName).getAbsolutePath());
                Clip clip = AudioSystem.getClip();
                clip.open(audioInputStream);
                clip.start();
            } catch (Exception e1) {
                System.out.println("Error.");
            }

        }
        if (e.getSource().equals(stopButton)) {
            System.out.println("Stop music.");
        }
        if (e.getSource().equals(pauseButton)) {
            System.out.println("Pause music.");
        }

    }
}
2

There are 2 answers

0
Phil Freihofner On

My first step in trying to solve this would be to manually inspect the properties of the wav file itself. In Windows, you can right-click and select properties. Then, there is a second tab that has things like bit rate and fps.

Java can support 44100 fps, and 16-bit encoding. There are DAWs now, though, that produce wav files at 48000 or 96000 fps, or at 24-bit or 32-bit encoding. Java does not support these currently, AFAIK.

If you have such a file, you can easily modify it into an acceptable format (stereo, 16-bit, 44100fps) with the program Audacity, which is a free download. Just make sure you get it from their home site. The first time I downloaded it, it was Popuppalooza for several hours until I was able to get rid of all the crapware that came with. The home site download (see wikipedia for the link) should be fine.

0
Witold Budzynski On

Check your audio file format. All supported in Your system formats can check by javax.sound.sampled.AudioSystem.getAudioFileTypes()