/** * Applet to play audio URL * * R. Perry, March 2005 */ import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; import java.applet.*; public class audio extends Applet implements ActionListener { // GUI // Button bstart, bstop; TextArea output; // network // String base, file; URL url; AudioClip a; public void init() { output = new TextArea(); output.setEditable( false); output.setBackground( Color.white); bstart = new Button( "Start"); bstop = new Button( "Stop"); GridBagLayout gridBag = new GridBagLayout(); setLayout( gridBag); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.BOTH; c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 1.0; c.weighty = 1.0; gridBag.setConstraints( output, c); add( output); c.fill = GridBagConstraints.HORIZONTAL; c.weighty = 0.0; c.gridwidth = 1; gridBag.setConstraints( bstart, c); add( bstart); gridBag.setConstraints( bstop, c); add( bstop); validate(); bstart.addActionListener(this); bstop.addActionListener(this); } public void start() { bstart.setEnabled( false); bstop.setEnabled( true); try { String newfile = getParameter( "file"); if( base == null) { base = getParameter( "base"); output.append( "Base for audio files is:\n" + base + "\n"); } if( file == null || file.compareTo(newfile) != 0) { file = newfile; output.append( "File is: " + file + "\n"); if( url == null) url = new URL( base); a = getAudioClip( url, file); } output.append( "Playing...\n"); a.loop(); } catch (Exception ex) { ByteArrayOutputStream error = new ByteArrayOutputStream(); ex.printStackTrace( new PrintStream( error)); output.append( error.toString()); } } public void stop() { if( a != null) a.stop(); bstart.setEnabled( true); bstop.setEnabled( false); } // handle action events // public void actionPerformed( ActionEvent e) { Object source = e.getSource(); try { if( source == bstart) { if( a != null) a.stop(); output.append( "Playing...\n"); if( a != null) a.loop(); bstart.setEnabled( false); bstop.setEnabled( true); } else if( source == bstop) { output.append( "Stop.\n"); stop(); } } catch (Exception ex) { ByteArrayOutputStream error = new ByteArrayOutputStream(); ex.printStackTrace( new PrintStream( error)); output.append( error.toString()); } } }