Program to Play Sound using Applet
Implementation of the SoundApplet program is mentioned below:
Java
// Java Program to play sound using Applet // Importing necessary packages import java.applet.Applet; import java.applet.AudioClip; import java.awt.Button; import java.awt.Color; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Label; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; // Defining the class public class SoundApplet extends Applet implements ActionListener { private AudioClip soundClip; private Button playBtn; private Button pauseBtn; private Button restartBtn; private Label status; public void init(){ // Loading the audio clip (replace "sound.wav" with // your sound file) soundClip = getAudioClip(getCodeBase(), "sound.wav" ); // Setting up the layout manager to FlowLayout setLayout( new FlowLayout()); // Creating the UI buttons for control playBtn = new Button( "Play" ); pauseBtn = new Button( "Pause" ); restartBtn = new Button( "Restart" ); // Customizing the button look Font buttonFont = new Font( "Arial" , Font.BOLD, 14 ); playBtn.setFont(buttonFont); pauseBtn.setFont(buttonFont); restartBtn.setFont(buttonFont); playBtn.setBackground(Color.GREEN); pauseBtn.setBackground(Color.RED); restartBtn.setBackground(Color.BLUE); playBtn.setForeground(Color.WHITE); pauseBtn.setForeground(Color.WHITE); restartBtn.setForeground(Color.WHITE); // Adding the action listeners to the buttons playBtn.addActionListener( this ); pauseBtn.addActionListener( this ); restartBtn.addActionListener( this ); // Adding buttons and label to the applet window add(playBtn); add(pauseBtn); add(restartBtn); // Creating a label for sound preview status = new Label( "Sound Preview: Click 'Play'" ); add(status); } public void actionPerformed(ActionEvent e){ if (e.getSource() == playBtn) { if (soundClip != null ) { soundClip.play(); status.setText( "Sound Preview: Playing" ); } } else if (e.getSource() == pauseBtn) { if (soundClip != null ) { soundClip.stop(); status.setText( "Sound Preview: Paused" ); } } else if (e.getSource() == restartBtn) { if (soundClip != null ) { soundClip.stop(); soundClip.play(); status.setText( "Sound Preview: Restarted" ); } } } } |
Output for the program
Explanation of the Program
- We have imported the packages that are necessary to define the Applet and the AWT components like Buttons, Labels, etc.
- Then we have defined the main SoundApplet class which is been implemented in the ActionListener interface.
- There is an init() method, in which all the sound controlling logic is been done, we have created the UI components also customized them. For each of the buttons like Play, Pause, and Restart, we have added the actionListener(). If any certain action is been performed by the user, then the functionality will be performed using actionListener().
- When the user clicks on the button, the status of the song is been printed using the Label. If the user clicks on the Play button, the sound is been played, along with this, the status of the sound is been shown as Playing.
How to Play Sound using Applet?
In this article, we will be using the Applet window to play the sound or music. Here, we will have buttons like Play, Pause, and Restart. Using these buttons we will manage the sound in the Applet window.