public class LoadAudioAndPlay extends JApplet
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
88:
89: /**************************************************************************
90: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
91: * Pearson Education, Inc. All Rights Reserved. *
92: * *
93: * DISCLAIMER: The authors and publisher of this book have used their *
94: * best efforts in preparing the book. These efforts include the *
95: * development, research, and testing of the theories and programs *
96: * to determine their effectiveness. The authors and publisher make *
97: * no warranty of any kind, expressed or implied, with regard to these *
98: * programs or to the documentation contained in these books. The authors *
99: * and publisher shall not be liable in any event for incidental or *
100: * consequential damages in connection with, or arising out of, the *
101: * furnishing, performance, or use of these programs. *
102: *************************************************************************/