Reputation: 15
So for ActionListener practice Im trying to make a program that keeps track of how many times i die to this enemy in a video game. Each time the button on the GUI is pressed it increases the counter by 1 and it also is supposed to play a sound.
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.*;
public class Main implements ActionListener {
int bad = 0;
private static JFrame frame;
private static JPanel panel;
private static JLabel bText;
private static JLabel damn;
private static JButton button;
private static Clip clip;
public static void main(String[] args) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
File file = new File("noise.wav");
AudioInputStream audioStream = AudioSystem.getAudioInputStream(file);
clip = AudioSystem.getClip();
clip.open(audioStream);
frame = new JFrame("times killed");
panel = new JPanel();
frame.setSize(420,420);
frame.add(panel);
panel.setLayout(null);
bText = new JLabel("Times killed:");
bText.setBounds(150,130,120,25);
panel.add(bText);
damn = new JLabel("0");
damn.setBounds(210,158,120,25);
panel.add(damn);
button = new JButton("Lmao you bad");
button.setBounds(140,190,150,25);
button.addActionListener(new Main());
panel.add(button);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
clip.start();
clip.setMicrosecondPosition(0);
bad++;
damn.setText("" + bad);
}
}
However when i run the program i get the error:
Exception in thread "main" java.io.FileNotFoundException: noise.wav (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:216)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
at java.desktop/com.sun.media.sound.SunFileReader.getAudioInputStream(SunFileReader.java:117)
at java.desktop/javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1060)
at Main.main(Main.java:22)
The audio file is definitely in the source folder however and i need it there so when i make it into a jar file it brings the audio file with it.
Upvotes: -2
Views: 61
Reputation: 92
I think you should try moving the .wav file outside of the src folder, in the main project directory.
Upvotes: 0