Reputation: 25
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!
Upvotes: 0
Views: 143
Reputation: 38
I'd say your actionListener should play the Music, instead of the constructor. Hence, the sound is played on event not on construction. I think MarkBernard has a very good point.
Upvotes: 0
Reputation: 1420
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);
}
}
Upvotes: 1
Reputation: 11
What about playing the music in a separate thread when you click on the button? So the actionPerformed of the action listener would start the thread to play the music.
Upvotes: 0