Open In App

How to Play Sound using Applet?

Last Updated : 23 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Approach for Playing Sound Using Applet

  • We need to import the essential packages like Applet and AWT for customization.
  • When the packages get imported, then we need to create the class that implemented the ActionListener interface in Java.
  • Once the class is been created, we need to initiate UI components like AudioClip, Button, and Lablel. All these will be the main UI components displayed on the screen.
  • After initialization of the components, we will define the init() method, in which the actual sound will be loaded from the local disk, and then we will create the Buttons to control the sound.
  • As there are buttons on the screen, we need to customize the appearance, and layout of these buttons. After customization, for each button, we will implement the action Listener, which will perform the desired functionality, as per user action.

Implementing the Applet

The folder view of the project is shown in the below image.

PS

Step 1: First, create the file as SoundApplet.java and enter the code into it.

Step 2: Once the code is been inserted then we need to create the AppletImage.html file, through which we will display the output.

HTML




<!DOCTYPE html>
<html>
<head>
    <title>Sound Applet</title>
</head>
<body>
    <applet code="SoundApplet.class" width="300" height="300">
    </applet>
</body>
</html>


Step 3: We need to have the sample sound file which we need to manage from applet. Here, we have the sound.wav file which is present in the code directory.

Step 4: Then we need to compile the Java code using the below command:

javac SoundApplet.java

Step 5: Now finally we need to run the application using the below command:

appletviewer SoundApplet.html

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.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads