This is a Java Program to Play Sound Using Applet
We have to write a program in Java such that it creates a music player to pause, play, stop and replay a sound.
The audio file is saved as “audio.wav”
Consider buttons are created with the following image icons :
For playing sound using applet, we can have the following 5 different sets of input and output.
1. To Play the Sound : Before the sound is Paused
When the Play button is clicked, it is expected that the sound is played.
2. To Pause the Sound :
When the Pause button is clicked and sound is being played, it is expected that the sound is paused.
3. To Play the Sound : After the sound is Paused
When the Play button is clicked, it is expected that the sound is played from the paused position.
4. To Stop the Sound :
When the Stop button is clicked, it is expected that the sound is stopped.
5. To Replay the Sound :
When the Replay button is clicked, it is expected that the sound is started from the beginning.
1. Create buttons with icons for the options – pause, play, stop and replay.
2. Load the sound file using AudioInputStream.
3. Create a object of class Clip to load the sound in a data line.
4. When the button pause is clicked, stop the clip and store the last position of the clip.
5. When the button play is clicked, start the clip from the last position of being stopped. In case the sound is never stopped, play from the beginning.
6. When the button stop is clicked, stop the clip and reset the last position to the beginning.
7. When the button replay is clicked, start the clip from the beginning.
Here is source code of the Java Program to play a sound using an applet. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.
/* Java Program to Play Sound using Applet */
public class Music extends Applet implements ActionListener
//Initialize the applet
public void init()
//Create image icons for the buttons
pause = new ImageIcon("icon_pause.png");
play = new ImageIcon("icon_play.png");
stop = new ImageIcon("icon_stop.png");
replay = new ImageIcon("icon_replay.png");
//Add buttons to applet and load the sound
public void start()
JButton b_pause = new JButton(pause);
JButton b_play = new JButton(play);
JButton b_stop = new JButton(stop);
JButton b_replay = new JButton(replay);
File file = new File ("sound.wav");
audio = AudioSystem.getAudioInputStream(file);
clip = AudioSystem.getClip();
//Function to perform the selected option
public void actionPerformed(ActionEvent e)
String icon = ((JButton)e.getSource()).getIcon().toString();
current = clip.getMicrosecondPosition();
current = 0L;
current = 0L;
<applet code = Music.java width=750 height=400>
To compile and execute the program use the following commands :
>>> javac Music.java >>> appletviewer Music.java
1. The class AudioInputStream is used to load an audio file.
2. To load an audio file to stream, use AudioSystem.getAudioInputStream(File).
3. To convert stream to data line, use AudioSystem.getClip().
4. To get the current position of the clip, use getMicrosecondPosition method.
5. To set the position of the clip, use setMicrosecondPosition(long) method.
6. To start the audio, use clip.start()
7. To stop the audio, use clip.stop()
Here’s the run time test cases for playing a sound using an applet.
Sanfoundry Global Education & Learning Series – Java Programs.