FileDocCategorySizeDatePackage
AudioAccessory.javaAPI DocExample2793Mon Nov 09 12:45:48 GMT 1998None

AudioAccessory

public class AudioAccessory extends JPanel implements PropertyChangeListener

Fields Summary
AudioClip
currentClip
String
currentName
JLabel
fileLabel
JButton
playButton
JButton
stopButton
Constructors Summary
public AudioAccessory()


    
    // Set up the accessory.  The file chooser will give us a reasonable size.
    setLayout(new BorderLayout());
    add(fileLabel = new JLabel("Clip Name"), BorderLayout.NORTH);
    JPanel p = new JPanel();
    playButton = new JButton("Play");
    stopButton = new JButton("Stop");
    playButton.setEnabled(false);
    stopButton.setEnabled(false);
    p.add(playButton);
    p.add(stopButton);
    add(p, BorderLayout.CENTER);

    playButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (currentClip != null) {
          currentClip.stop();
          currentClip.play();
        }
      }
    });
    stopButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        if (currentClip != null) {
          currentClip.stop();
        }
      }
    });
  
Methods Summary
public voidpropertyChange(java.beans.PropertyChangeEvent e)

    if (e.getPropertyName()
         .equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {

      // Ok, the user selected a file in the chooser
      File f = (File)e.getNewValue();

      // Make reasonably sure it's an audio file
      if (f.getName().toLowerCase().endsWith(".au")) {
        setCurrentClip(f);
      }
      else {
        setCurrentClip(null);
      }
    }
  
public voidsetCurrentClip(java.io.File f)

    // Make sure we have a real file, otherwise, disable the buttons
    if ((f == null) || (f.getName() == null)) {
      fileLabel.setText("no audio selected");
      playButton.setEnabled(false);
      stopButton.setEnabled(false);
      return;
    }

    // Ok, seems the audio file is real, so load it and enable the buttons
    String name = f.getName();
    if (name.equals(currentName)) {
      return;
    }
    if (currentClip != null) { currentClip.stop(); }
    currentName = name;
    try {
      URL u = new URL("file:///" + f.getAbsolutePath());
      currentClip = new AppletAudioClip(u);
    }
    catch (Exception e) {
      e.printStackTrace();
      currentClip = null;
      fileLabel.setText("Error loading clip.");
    }
    fileLabel.setText(name);
    playButton.setEnabled(true);
    stopButton.setEnabled(true);