Loading and Playing Audio Clips

Java programs can manipulate and play audio clips. Users can capture their own audio clips, and many clips are available in software products and over the Internet. Your system needs to be equipped with audio hardware (speakers and a sound card) to be able to play the audio clips.

Java provides several mechanisms for playing sounds in an applet. The two simplest are the Applet's play method and the play method of the AudioClip interface. Additional audio capabilities are available in the Java Media Framework and Java Sound APIs. If you would like to play a sound once in a program, the Applet method play loads the sound and plays it oncethe sound is marked for garbage collection after it plays. The Applet method play has two versions:


 

public void play( URL location, String soundFileName ); public void play( URL soundURL );

The first version loads the audio clip stored in file soundFileName from location and plays the sound. The first argument is normally a call to the applet's getdocumentBase or getCodeBase method. Method geTDocumentBase returns the location of the HTML file that loaded the applet. (If the applet is in a package, the method returns the location of the package or the JAR file containing the package.) Method getCodeBase indicates the location of the applet's .class file. The second version of method play takes a URL that contains the location and the file name of the audio clip. The statement

 play( getDocumentBase(), "hi.au" );

loads the audio clip in file hi.au and plays the clip once.

The sound engine that plays the audio clips supports several audio file formats, including Sun Audio file format (.au extension), Windows Wave file format (.wav extension), Macintosh AIFF file format (.aif or .aiff extensions) and Musical Instrument Digital Interface (MIDI) file format (.mid or .rmi extensions). The Java Media Framework (JMF) and Java Sound APIs support additional formats.

The program of Fig. 21.5 demonstrates loading and playing an AudioClip (package java.applet). This technique is more flexible than Applet method play. An applet can use an AudioClip to store audio for repeated use throughout a program's execution.

Figure 21.5. Loading and playing an AudioClip.

(This item is displayed on pages 991 - 992 in the print version)

 1 // Fig. 21.5: LoadAudioAndPlay.java
 2 // Load an audio clip and play it.
 3 import java.applet.AudioClip;
 4 import java.awt.event.ItemListener;
 5 import java.awt.event.ItemEvent;
 6 import java.awt.event.ActionListener;
 7 import java.awt.event.ActionEvent;
 8 import java.awt.FlowLayout;
 9 import javax.swing.JApplet;
10 import javax.swing.JButton;
11 import javax.swing.JComboBox;
12
13 public class LoadAudioAndPlay extends JApplet
14 {
15 private AudioClip sound1, sound2, currentSound;
16 private JButton playJButton, loopJButton, stopJButton;
17 private JComboBox soundJComboBox;
18
19 // load the image when the applet begins executing
20 public void init()
21 {
22 setLayout( new FlowLayout() );
23
24 String choices[] = { "Welcome", "Hi" };
25 soundJComboBox = new JComboBox( choices ); // create JComboBox
26
27 soundJComboBox.addItemListener(
28
29 new ItemListener() // anonymous inner class
30 {
31 // stop sound and change to sound to user's selection
32 public void itemStateChanged( ItemEvent e )
33 {
34 currentSound.stop();
35 currentSound = soundJComboBox.getSelectedIndex() == 0 ?
36 sound1 : sound2;
37 } // end method itemStateChanged
38 } // end anonymous inner class
39 ); // end addItemListener method call
40
41 add( soundJComboBox ); // add JComboBox to applet
42
43 // set up button event handler and buttons
44 ButtonHandler handler = new ButtonHandler();
45
46 // create Play JButton
47 playJButton = new JButton( "Play" );
48 playJButton.addActionListener( handler );
49 add( playJButton );
50
51 // create Loop JButton
52 loopJButton = new JButton( "Loop" );
53 loopJButton.addActionListener( handler );
54 add( loopJButton );
55
56 // create Stop JButton
57 stopJButton = new JButton( "Stop" );
58 stopJButton.addActionListener( handler );
59 add( stopJButton );
60
61 // load sounds and set currentSound
62 sound1 = getAudioClip( getDocumentBase(), "welcome.wav" );
63 sound2 = getAudioClip( getDocumentBase(), "hi.au" ); 
64 currentSound = sound1;
65 } // end method init
66
67 // stop the sound when the user switches Web pages
68 public void stop()
69 {
70 currentSound.stop(); // stop AudioClip
71 } // end method stop
72
73 // private inner class to handle button events
74 private class ButtonHandler implements ActionListener
75 {
76 // process play, loop and stop button events
77 public void actionPerformed( ActionEvent actionEvent )
78 {
79 if ( actionEvent.getSource() == playJButton )
80 currentSound.play(); // play AudioClip once
81 else if ( actionEvent.getSource() == loopJButton )
82 currentSound.loop(); // play AudioClip continuously
83 else if ( actionEvent.getSource() == stopJButton )
84 currentSound.stop(); // stop AudioClip
85 } // end method actionPerformed
86 } // end class ButtonHandler
87 } // end class LoadAudioAndPlay
 

Applet method getAudioClip has two forms that take the same arguments as method play described previously. Method getAudioClip returns a reference to an AudioClip. An AudioClip has three methodsplay, loop and stop. As mentioned earlier, method play plays the audio clip once. Method loop continuously loops through the audio clip in the background. Method stop terminates an audio clip that is currently playing. In the program, each of these methods is associated with a button on the applet.

Lines 6263 in the applet's init method use getAudioClip to load two audio filesa Windows Wave file (welcome.wav) and a Sun Audio file (hi.au). The user can select which audio clip to play from the JComboBox soundJComboBox. Note that the applet's stop method is overridden at lines 6871. When the user switches Web pages, the applet container calls the applet's stop method. This enables the applet to stop playing the audio clip. Otherwise, it continues to play in the backgroundeven if the applet is not displayed in the browser. This is not necessarily a problem, but it can be annoying to the user if the audio clip is looping. The stop method is provided here as a convenience to the user.

Look-and-Feel Observation 21.5

When playing audio clips in an applet or application, provide a mechanism for the user to disable the audio.



Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look

Arrays

Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams

Recursion

Searching and Sorting

Data Structures

Generics

Collections

Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2

Multithreading

Networking

Accessing Databases with JDBC

Servlets

JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover



Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net