-2

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.");
        }

    }
}
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Kharbora
  • 129
  • 1
  • 8
  • 1
    change System.out.println("Error."); to e1.printStrackTrace(); and find the error – Madhawa Priyashantha Nov 19 '14 at 09:56
  • 3
    Where is the file stored in relationship to the classes? Avoid using `null` layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify – MadProgrammer Nov 19 '14 at 09:58
  • 1
    please make sure that the .wav file under the project root. and it's PCM wav format .. Also it would be great to send us the output of e1.printStackTrace() if the wav file will be packaged in the jar. so you can get it by this.getClass().getResourceAsStream("gangnam.wav") – Muhammad Hamed Nov 19 '14 at 10:03
  • @MuhammadHamed *"..make sure that the .wav file .. (is) PCM wav format.."* I'm betting it is **not** (that format that Java Sound understands..). – Andrew Thompson Nov 19 '14 at 16:12
  • @AndrewThompson: check this http://stackoverflow.com/questions/16002372/play-a-raw-pcm-byte-from-a-wav-file – Muhammad Hamed Nov 19 '14 at 19:03
  • 4
    There is no sense in vandalizing your own post, you will just have your account banned. And someone will always roll it back within seconds. – Tunaki Oct 24 '15 at 21:06

2 Answers2

1

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.

Phil Freihofner
  • 7,645
  • 1
  • 20
  • 41
0

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