I am trying to play an audio file in the background of a Frame window and I'm finding out it is way more complicated then just adding a picture.
I found a "tutorial" that should how to add music and it seemed fairly simple, at least in the amount of code required. However, I am not able to play the audio file, it is saying two things...
1st - I was trying to play a audio file I copied to my java project and that was telling me it could not find that file or directory.
2nd - I gave the method a path to a audio file and it's telling me...
could not get audio input stream from input file
Please keep in mind I'm very new and this tutorial did not give a lot of help, so I do not know a lot of what this play method is actually doing. I'd love to learn how to do this but everything I've seen is overly complicated for this project and I don't have the time to put in because it's not necessary just a little added flair is all.
Any help is greatly appreciated!! I put stars around the code in question.
public MultiForm() {
super("Multi Form Program");
setLayout(new FlowLayout());
menu = new JComboBox(fileName);
add(menu);
/*How to add a background image to the Menu.
* add(new JLabel(new ImageIcon(getClass().getResource("MatrixPIC.png"))));
*/
TheHandler handler = new TheHandler();
menu.addItemListener(handler);
}
public void matrixPanel() {
TheHandler handler = new TheHandler();
//Create a new window when "The Matrix" is clicked in the JCB
newFrame = new JFrame();
panel = new JPanel();
panel2 = new JPanel();
newFrame.setLayout(new FlowLayout());
newFrame.setSize(500, 300);
newFrame.setDefaultCloseOperation(newFrame.EXIT_ON_CLOSE);
matrixQuote = new JLabel("<html><center>After this, there is no turning back. "
+ "<br><center>You take the blue pill—the story ends, you wake up "
+ "<br><center>in your bed and believe whatever you want to believe."
+ "<br><center>You take the red pill—you stay in Wonderland, and I show"
+ "<br><center>you how deep the rabbit hole goes. "
+ "<br><center>Remember: all I'm offering is the truth. Nothing more.</html>");
panel.add(matrixQuote);
newFrame.add(panel, BorderLayout.NORTH);
//Blue pill button and picture.
Icon bp = new ImageIcon(getClass().getResource("Blue Pill.png"));
bluePill = new JButton("Blue Pill", bp);
panel2.add(bluePill);
bluePill.addActionListener(handler);
//Red pill button and picture
Icon rp = new ImageIcon(getClass().getResource("Red Pill.png"));
redPill = new JButton("Red Pill", rp);
panel2.add(redPill);
newFrame.add(panel2, BorderLayout.CENTER);
newFrame.setVisible(true);
}
*********************************************************************************
public void play(String path, int delay, int numberOfLoops) {
for(int i = 0; i < numberOfLoops; i++) {
new Thread() {
@Override
public void run() {
try {
File file = new File(path);
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(file));
clip.start();
Thread.sleep(clip.getMicrosecondLength());
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}.start();
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
*********************************************************************************
private class TheHandler implements ItemListener, ActionListener{
public void itemStateChanged(ItemEvent IE) {
//listen for an item to be selected.
if(IE.getStateChange() == ItemEvent.SELECTED) {
Object selection = menu.getSelectedItem();
if("The Matrix".equals(selection)) {
matrixPanel();
}
else if("Another Option".equals(selection)) {
}
}
}
public void actionPerformed(ActionEvent AE) {
if(AE.getSource() == bluePill) {
//Clear panels after button is clicked.
newFrame.remove(panel);
newFrame.remove(panel2);
newFrame.repaint();
*********************************************************************************
play("/Users/SWD/Downloads/fail.mp3", 10, 30);
*********************************************************************************
newFrame.setSize(600, 400);
bpPanelLabel = new JPanel();
bpLabel = new JLabel("<html><center>WELCOME TO THE BEGINING OF YOUR NEW LIFE! " +
"<br><center>YOU'RE ABOUT TO SEE HOW DEEP THE RABBIT HOLE GOES!</html>");
newFrame.add(bpLabel);
newFrame.add(bpPanelLabel, BorderLayout.NORTH);
bpPanelPic = new JPanel();
newFrame.add(new JLabel(new ImageIcon(getClass().getResource("MatrixPIC.png"))));
newFrame.add(bpPanelPic, BorderLayout.CENTER);
newFrame.validate();
}
}
}
//Main method sets up the main JFrame with the menu
public static void main(String[] args) {
MultiForm go = new MultiForm();
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(400, 200);
go.setVisible(true);
}
}