play sound Example
This applet plays a .au sound file when a button is pressed. Notice that the setting up is done in init() so that a potentially large sound file is loaded just once.
The AudioClip interface has methods for play() stop() and loop().
// play a sound in an applet using AudioClip
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class PlaySoundExample extends JApplet implements ActionListener {
AudioClip sound;
@Override public void init() {
sound = getAudioClip(getDocumentBase(), "sounds/drip.au");
Container cp = getContentPane();
JButton bp = new JButton("play");
bp.addActionListener(this);
cp.add(bp);
}
public void actionPerformed(ActionEvent e) {
String a = e.getActionCommand();
if(a.equals("play")) sound.play();
}
}
