/Users/lyon/j4p/src/sound/player/SoundApplication.java

1    package sound.player; 
2     
3    import futils.Futil; 
4    import gui.In; 
5     
6    import javax.swing.*; 
7    import java.applet.AudioClip; 
8    import java.awt.*; 
9    import java.awt.event.*; 
10   import java.net.MalformedURLException; 
11   import java.net.URL; 
12    
13   public class SoundApplication extends JPanel 
14           implements ActionListener, 
15           ItemListener { 
16       SoundList soundList; 
17       String auFile = "spacemusic.au"; 
18       String aiffFile = "trippygaia1.mid"; 
19       String midiFile = "trippygaia1.mid"; 
20       String rmfFile = "trippygaia1.mid"; 
21       String wavFile = "trippygaia1.mid"; 
22       String chosenFile; 
23    
24       AudioClip onceClip, loopClip; 
25       URL codeBase; 
26    
27       JComboBox formats; 
28       JButton playButton, loopButton, stopButton; 
29       JLabel status; 
30    
31       boolean looping = false; 
32    
33       public SoundApplication() { 
34           String[] fileTypes = {auFile, 
35                                 aiffFile, 
36                                 midiFile, 
37                                 rmfFile, 
38                                 wavFile}; 
39           formats = new JComboBox(fileTypes); 
40           formats.setSelectedIndex(0); 
41           chosenFile = (String) formats.getSelectedItem(); 
42           formats.addItemListener(this); 
43    
44           playButton = new JButton("Play"); 
45           playButton.addActionListener(this); 
46    
47           loopButton = new JButton("Loop"); 
48           loopButton.addActionListener(this); 
49    
50           stopButton = new JButton("Stop"); 
51           stopButton.addActionListener(this); 
52           stopButton.setEnabled(false); 
53    
54           status = new JLabel("Click Play or Loop to play the selected sound file."); 
55    
56           JPanel controlPanel = new JPanel(); 
57           controlPanel.add(formats); 
58           controlPanel.add(playButton); 
59           controlPanel.add(loopButton); 
60           controlPanel.add(stopButton); 
61    
62           JPanel statusPanel = new JPanel(); 
63           statusPanel.add(status); 
64    
65           add(controlPanel); 
66           add(statusPanel); 
67    
68           startLoadingSounds(); 
69       } 
70    
71       public void itemStateChanged(ItemEvent e) { 
72           chosenFile = (String) formats.getSelectedItem(); 
73           soundList.startLoading(chosenFile); 
74       } 
75    
76       void startLoadingSounds() { 
77           //Start asynchronous sound loading. 
78           try { 
79               String s = "file://" + 
80                       Futil.getReadDirFile("select an audio file") + "/"; 
81               codeBase = new URL(s); 
82               In.message("codebase=" + codeBase); 
83           } catch (MalformedURLException e) { 
84               System.err.println(e.getMessage()); 
85           } 
86           soundList = new SoundList(codeBase); 
87           soundList.startLoading(auFile); 
88           soundList.startLoading(aiffFile); 
89           soundList.startLoading(midiFile); 
90           soundList.startLoading(rmfFile); 
91           soundList.startLoading(wavFile); 
92       } 
93    
94       public void stop() { 
95           onceClip.stop();        //Cut short the one-time sound. 
96           if (looping) { 
97               loopClip.stop();    //Stop the sound loop. 
98           } 
99       } 
100   
101      public void start() { 
102          if (looping) { 
103              loopClip.loop();    //Restart the sound loop. 
104          } 
105      } 
106   
107      public void actionPerformed(ActionEvent event) { 
108          //PLAY BUTTON 
109          Object source = event.getSource(); 
110          if (source == playButton) { 
111              //Try to get the AudioClip. 
112              onceClip = soundList.getClip(chosenFile); 
113              stopButton.setEnabled(true); 
114              onceClip.play();     //Play it once. 
115              status.setText("Playing sound " + chosenFile + "."); 
116              if (onceClip == null) { 
117                  status.setText("Sound " + chosenFile + " not loaded yet."); 
118              } 
119              return; 
120          } 
121   
122          //START LOOP BUTTON 
123          if (source == loopButton) { 
124              loopClip = soundList.getClip(chosenFile); 
125   
126              looping = true; 
127              loopClip.loop();     //Start the sound loop. 
128              loopButton.setEnabled(false); //Disable start button. 
129              stopButton.setEnabled(true); 
130              status.setText("Playing sound " + chosenFile + " continuously."); 
131              if (loopClip == null) { 
132                  status.setText("Sound " + chosenFile + " not loaded yet."); 
133              } 
134              return; 
135          } 
136   
137          //STOP LOOP BUTTON 
138          if (source == stopButton) { 
139              if (looping) { 
140                  looping = false; 
141                  loopClip.stop();    //Stop the sound loop. 
142                  loopButton.setEnabled(true); //Enable start button. 
143              } else if (onceClip != null) { 
144                  onceClip.stop(); 
145              } 
146              stopButton.setEnabled(false); 
147              status.setText("Stopped playing " + chosenFile + "."); 
148              return; 
149          } 
150      } 
151   
152      public static void main(String s[]) { 
153          WindowListener l = new WindowAdapter() { 
154              public void windowClosing(WindowEvent e) { 
155                  System.exit(0); 
156              } 
157          }; 
158          JFrame f = new JFrame("SoundApplication"); 
159          f.addWindowListener(l); 
160          f.getContentPane().add(new SoundApplication()); 
161          f.setSize(new Dimension(400, 100)); 
162          f.show(); 
163      } 
164  } 
165