This applet program plays an audio. The program declares objects sf1 and sf2 of AudoClip class. The program uses two audio clips. The getAudioClip() function is used to fetch the audio file from the disk, it will be stored in the objects sf1 and sf2. The play() function is used to play the audio clip. When the program first loads the function sf1.play(); will be invoked and the first audio clip will be played. When the user clicks somewhere on the window, the first audio clip will be stopped and the second audio clip will be played. Also both the audio files must be stored in the same directory where the source file if stored.
Program
//Applet program in java to add sound/Audio //<Applet code=SoundApplet height=400 width=400></Applet> import java.applet.*; import java.awt.*; import java.awt.event.*; public class SoundApplet extends Applet implements MouseListener { AudioClip sf1,sf2; public void start() { System.out.println("Applet Started"); } public void init() { sf1=getAudioClip(getCodeBase(),"doorbell.wav"); sf2=getAudioClip(getCodeBase(),"doorbell.wav"); addMouseListener(this); setBackground(Color.BLUE); sf1.play(); } public void paint(Graphics g) { g.drawString("Sound",100,200); } public void mouseClicked(MouseEvent e) { sf2.play(); } public void mouseReleased(MouseEvent e) { System.out.println("Mouse Released"); } public void mousePressed(MouseEvent e) { System.out.println("Mouse Pressed"); } public void mouseEntered(MouseEvent e) { System.out.println("Mouse Entered"); } public void mouseExited(MouseEvent e) { System.out.println("Mouse Exited"); } }