So I'm trying to incorporate sound using another class so that I can later on use it in a game, but I have no idea how to do anything without getting an error, this is what I've got so far
Main class (Project1.class)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Project1 {
public static void main(String[] args) throws Exception{
Music m = new Music();
m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
m.setSize(300,300);
JButton button = new JButton("Click here for 4 second part of the music");
m.add(button);
button.addActionListener(new AL());
m.setVisible(true);
}
public static class AL implements ActionListener{
public final void actionPerformed(ActionEvent e){
//What do I put here so that I could play the Music from the other class?
}
}
}
And here's the class which actually plays the music (Music.class)
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import javax.swing.JButton;
import javax.swing.JFrame;
import sun.audio.*;
import sun.*;
public class Music extends JFrame{
private JButton button;
public Music() throws Exception {
super("The title");
String filename = "/Users/seb/Documents/workspace(NormalJava)/practs/res/backgroundMusic.wav";
InputStream in = new FileInputStream(new File(filename));
AudioStream audioStream = new AudioStream(in);
AudioPlayer.player.start(audioStream);
Thread.sleep(4000);
AudioPlayer.player.stop(audioStream);
}
}
If anyone could help I'd be very grateful, I've never done sound and I have no idea how to do it and I'm VERY confused about this, what would I put in the button ActionListener class so that only whenever i press it I get the music to start, and then stop after 4 seconds? If I put the Thread.sleep(4000); in the Music class, then it starts the music, waits 4 seconds, stops and then shows me the button
So if anyone could help me get an idea about audio, or perhaps another, easier way of doing this. I'd be very grateful!
The reason the music plays first is because you have the play method in the constructor. So:
public static void main(String[] args) throws Exception{
Music m = new Music(); // ****** Music plays here
m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
m.setSize(300,300);
JButton button = new JButton("Click here for 4 second part of the music");
m.add(button);
button.addActionListener(new AL());
m.setVisible(true);
}
Then after all that you set your size, etc.
Everything you have in the main method should be in the constructor for Music(). Your music playing code should be in the ActionListener class AL.
You also need to make sure you don't tie up the event thread. So in your ActionListener you would have something like:
SwingUtilities.invokeLater(new Runnable() {
public void run() {
String filename = "/Users/seb/Documents/workspace(NormalJava)/practs/res/backgroundMusic.wav";
InputStream in = new FileInputStream(new File(filename));
AudioStream audioStream = new AudioStream(in);
AudioPlayer.player.start(audioStream);
Thread.sleep(4000);
AudioPlayer.player.stop(audioStream);
}
}