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