Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
ADDING SOUND TO JAVA APPLICATIONS It’s now time to add sound effects to our programs. Fortunately, Java provides built-in support for playing sound and music files. Java supports a variety of audio formats, including WAV files in several formats (WAV, AU, RMF, and AIFF) and MIDI files. WAV files are usually used to add specific sound effects to your program, whereas MIDI files let you play music while your program is running. PLAYING A SOUND FILE In order to get sound files to work, you will need to import the java.io and sun.audio packages. The java.io package is necessary since you will need to use the InputStream class to get the audio file that you want to use. The sun.audio package is necessary because you will need to use the AudioStream and AudioPlayer, and classes. The following program will include a PLAY button to start playing the sound file and a STOP button to stop playing the sound file. However, in this first example, we will not be looping the sound file; this will happen in the next section of the lesson. import import import import import java.io.*; sun.audio.*; javax.swing.*; java.awt.event.*; java.awt.*; public class SoundExample extends JFrame implements ActionListener { private private private private JButton btnPlay, btnStop; AudioStream auStream; AudioPlayer auPlayer; InputStream in; public static void main(String[] args) { new SoundExample(); } public SoundExample() { // Declare and initialize an AudioPlayer object auPlayer = AudioPlayer.player; // Add the components to your interface btnPlay = new JButton("PLAY"); btnPlay.setPreferredSize(new Dimension(100, 30)); Adding Sounds to Java Applications Page 1 of 5 btnPlay.setFocusable(false); btnPlay.addActionListener(this); btnStop = new JButton("STOP"); btnStop.setPreferredSize(new Dimension(100, 30)); btnStop.setFocusable(false); btnStop.addActionListener(this); JPanel panel = new JPanel(); panel.add(Box.createRigidArea(new Dimension(275, 30))); panel.add(btnPlay); panel.add(btnStop); setContentPane(panel); setSize(500, 150); setTitle("Playing Sounds"); setLocationRelativeTo(null); setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == btnPlay) { try { // Initialize the InputStream object with the sound file you want to use in = new FileInputStream(new File("glass_ping.wav")); // Initialize the AudioStream object by passing the InputStream object auStream = new AudioStream(in); } catch(IOException e) { // Catch the exception that gets thrown if the file cannot be retrieved System.out.println(e.getMessage()); } // Play the audio file auPlayer.start(auStream); } else { // Stop the audio file auPlayer.stop(auStream); } } Adding Sounds to Java Applications Page 2 of 5 When you run the program and click the PLAY button, the audio clip will play once for as long as the audio clip lasts. Each time the user clicks the PLAY button, the audio clip will play. LOOPING A SOUND FILE The above example only plays the audio clip once when the user clicks the START button. In some cases, you may want or need an audio clip to play continuously (for example, if you wanted to add background music to a game). Looping a sound file requires two additional objects: an AudioData object and a ContinuousAudioDataStream object (both of which are included in the sun.audio package. The following code will continuously loop an audio file once the user clicks the START button and stop the file from playing when the user clicks the STOP button: import import import import import java.io.*; sun.audio.*; javax.swing.*; java.awt.event.*; java.awt.*; public class SoundExample extends JFrame implements ActionListener { private private private private private private JButton btnStart, btnStop; AudioStream auStream; AudioPlayer auPlayer; InputStream in; AudioData auData; ContinuousAudioDataStream auLoop; public static void main(String[] args) { new SoundExample(); } public SoundExample() { // Declare and initialize an AudioPlayer object auPlayer = AudioPlayer.player; Adding Sounds to Java Applications Page 3 of 5 // Add the components to your interface btnStart = new JButton("START"); btnStart.setPreferredSize(new Dimension(100, 30)); btnStart.setFocusable(false); btnStart.addActionListener(this); btnStop = new JButton("STOP"); btnStop.setPreferredSize(new Dimension(100, 30)); btnStop.setFocusable(false); btnStop.addActionListener(this); JPanel panel = new JPanel(); panel.add(Box.createRigidArea(new Dimension(275, 30))); panel.add(btnStart); panel.add(btnStop); setContentPane(panel); setSize(300, 150); setLocationRelativeTo(null); setResizable(false); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == btnStart) { try { // Initialize the InputStream object with the sound file you want to use in = new FileInputStream(new File("glass_ping.wav")); // Initialize the AudioStream object by passing the InputStream object auStream = new AudioStream(in); // Initialize the AudioData object by calling the getData() method included in the AudioStream class auData = auStream.getData(); // Initialize the ContinuousAudioDataStream object by passing the AudioData object auLoop = new ContinuousAudioDataStream(auData); } catch(IOException e) { // Catch the exception that gets thrown if the file cannot be retrieved System.out.println(e.getMessage()); } Adding Sounds to Java Applications Page 4 of 5 // Play the audio file auPlayer.start(auLoop); } else { // Stop the audio file auPlayer.stop(auLoop); } } } Now when you run the program and click the START button, the audio file will play continuously until the user clicks the STOP button. Adding Sounds to Java Applications Page 5 of 5