// PLAYING SOUND in JAVA
//Copy and paste the code below to a separate class (SoundEffects class) inside your project.
//In order to make the sound work,
//just put the code: SoundEffects.song1.play();
//In order to make the sound stop type the code: SoundEffects.song1.halt();
//This only works for .wav or .aiff audio files, so you might have to convert in your audio software.
//Drag your audio file into the src folder inside the Project.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class PlayMusic extends JFrame {
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
PlayMusic frame = new PlayMusic();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public PlayMusic() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JButton btnPlay = new JButton("Play");
btnPlay.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SoundEffects.song1.play();
}
});
btnPlay.setBounds(161, 131, 117, 29);
contentPane.add(btnPlay);
}
}