public class AudioApplet4 extends Applet
1: //AudioApplet4.java
2: //Demonstrates repeated playing of sounds via an applet.
4: import java.awt.*;
5: import java.awt.event.*;
6: import java.applet.*;
8: public class AudioApplet4 extends Applet
9: implements ActionListener
10: {
11: static final int START = 0;
12: static final int STOP = 1;
14: //Initialize an array with the names of the sounds
15: String[] soundNames =
16: {"bark",
17: "computer",
18: "crash",
19: "cuckoo",
20: "doorbell",
21: "drip",
22: "gong",
23: "ring",
24: "spacemusic",
25: "train"};
27: //Initialize array with names of functions
28: String[] functions = {"Start","Stop"};
30: //Instantiate an array of buttons for sounds
31: Button[] button = new Button[soundNames.length];
33: //Instantiate an array of buttons for functions
34: Button[] functionButton = new Button[functions.length];
36: AudioClip sound;
37: String soundSelected = new String();
38: boolean selectionMade = false;
39: int selectedButton = 0;
40: int selectedFunction = 0;
42: //Override init() method to set up and display an array of buttons
43: //containing the names of the sounds, and an array of functions
44: public void init()
45: {
46: setLayout(null);
47: setBackground(Color.black);
49: for (int index=0; index!=soundNames.length; index++)
50: {
51: button[index] = new Button(soundNames[index]);
52: button[index].setLocation(10, 25*index+5);
53: button[index].setSize(100, 25);
54: button[index].setBackground(Color.cyan);
55: button[index].addActionListener(this);
56: add(button[index]);
57: }
58:
59: for (int index=0; index!=functions.length; index++)
60: {
61: functionButton[index] = new Button(functions[index]);
62: functionButton[index].setLocation(135,75*(index+1));
63: functionButton[index].setSize(100,40);
64: functionButton[index].setBackground(Color.white);
65: functionButton[index].addActionListener(this);
66: add(functionButton[index]);
67: }
69: }
71: //Implementation the actionPerformed method
72: //in the ActionListener interface
73: public void actionPerformed(ActionEvent event)
74: {
75: //Find the name of button that was pressed
76: Object source = event.getActionCommand();
78: //If a sound has been selected then getAudioClip
79: if (selectionMade)
80: sound = getAudioClip(getCodeBase(), soundSelected);
82: //Check which function button was pressed
83: for (int index=0; index!=functions.length; index++)
84: {
85: //Reset color of function button
86: functionButton[selectedFunction].setBackground(Color.white);
88: //Test to see if either a start or stop button was pressed
89: //and whether a sound has been selected
90: if (source.equals(functions[index]) && selectionMade)
91: {
92: //Change color of button to show function selected
93: functionButton[index].setBackground(Color.yellow);
94: selectedFunction = index;
96: //Implement function to start or stop the sound
97: switch (index)
98: {
99: case START: sound.loop(); return;
100: case STOP: sound.stop(); return;
101: }
102: }
103: }
104:
105: //Check which sound button was pressed
106: for (int index=0; index!=soundNames.length; index++)
107: {
108: //Reset color of sound buttons
109: button[selectedButton].setBackground(Color.cyan);
111: //Test to see which sound has been chosen
112: if (source.equals(soundNames[index]))
113: {
114: //Obtain sound file from directory
115: soundSelected = "audio/"+source.toString()+".au";
116: //Change color of button to show chosen item
117: button[index].setBackground(Color.yellow);
119: //Update flags to show which button was pressed
120: //and selection made
121: selectedButton = index;
122: selectionMade = true;
124: return;
125: }
126: }
127: }
128: }