I working on a program that both plays a sound when you click a button in an applet and in an application but I keep receiving an error while in the application portion of the program.
This is the error I get: sample.mp3 (The system cannot find the file specified) but I clearly have it in my project.
public class project extends JApplet {
public void init() {
add(new AppletOrApplicationMainComponent());
}
public static void main(String args[]) {
JFrame frame = new JFrame("");
frame.getContentPane().add(new AppletOrApplicationMainComponent());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
class AppletOrApplicationMainComponent extends JPanel {
public AppletOrApplicationMainComponent() {
super(new BorderLayout());
JButton button = new JButton("Play");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Play();
}
});
add(button, BorderLayout.SOUTH);
}
private void Play(){
try{
File file = new File("sample.mp3");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
Player player = new Player(bis);
player.play();
}catch(Exception e){
e.printStackTrace();
}
}
}