FileDocCategorySizeDatePackage
CoreJavaSound.javaAPI DocExample1948Mon Jan 09 11:02:00 GMT 2006None

CoreJavaSound

public class CoreJavaSound extends Object implements LineListener

Fields Summary
File
soundFile
JDialog
playingDialog
Clip
clip
Constructors Summary
public CoreJavaSound(File f)

        soundFile = f;
        // prepare a dialog to display while playing
        JOptionPane pane = new JOptionPane ("Playing " + f.getName(),
                                            JOptionPane.PLAIN_MESSAGE);
        playingDialog = pane.createDialog (null, "Application Sound");
        playingDialog.pack();

        // get and play sound
        Line.Info linfo = new Line.Info (Clip.class);
        Line line = AudioSystem.getLine (linfo);
        clip = (Clip) line;
        clip.addLineListener (this);
        AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile);
        clip.open (ais);
        clip.start();
    
Methods Summary
public static voidmain(java.lang.String[] args)

        JFileChooser chooser = new JFileChooser();
        chooser.showOpenDialog(null);
        File f = chooser.getSelectedFile();
        try {
            CoreJavaSound s = new CoreJavaSound (f);
        } catch (Exception e) {
            e.printStackTrace();
        }
    
public voidupdate(javax.sound.sampled.LineEvent le)

        LineEvent.Type type = le.getType();
        if (type == LineEvent.Type.OPEN) {
            System.out.println ("OPEN");
        } else if (type == LineEvent.Type.CLOSE) {
            System.out.println ("CLOSE");
            System.exit (0);
        } else if (type == LineEvent.Type.START) {
            System.out.println ("START");
            playingDialog.setVisible(true);
        } else if (type == LineEvent.Type.STOP) {
            System.out.println ("STOP");
            playingDialog.setVisible(false);
            clip.close();
        }