Open In App

How to Play Audio File (.WAV) Using Java Applet?

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

In this article, we will learn how to read and play audio (.wav) files using Java Applet.

Approach for Playing Audio File(.wav) using Applet

MusicPlayerApplet class inherits the property of Applet in Java. The interface consists of the input text field to enter a filename with an extension of the audio (.wav) file, a button to play the audio, and another button to stop the audio.

The AudioClip interface is used to play and stop the audio. The AudioClip interface consists of two important to play and stop the audio.

On clicking the Play button or Stop button, the action listener will call a method that contains logic to play and stop the audio. If the play button is pressed, play() method is called to play the audio. If the stop button is pressed, stop() method is called to stop the audio.

Notes:

Latest java version doesn’t support applet. so, it needs older version of java which supports applet to be installed.
AudioClip interface supports only WAV (Waveform Audio File Format) file with .wav extension. Other formats are not supported.

Java version for Applet viewer

Applet viewer is no longer available from Java version 9. So, it requires Java version 8 or older to run applet viewer. Refer to the following article to install the applet-supported Java version.How to Install Java Applet Viewer in Windows?

Program to play audio file (.WAV) using Applet

Below is the implementation of the above method:

Java




// Java Applet Program to demonstrate
// Play audio file (.WAV)  using Java Applet
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.applet.AudioClip;
  
// Driver Class
public class MusicPlayerApplet extends Applet implements ActionListener {
    private Button playButton;
    private Button stopButton;
    private TextField filenameField;
    private Label filenameLabel;    
    private AudioClip audioClip;
  
    public void init() {
  
        filenameField = new TextField(20);
        filenameLabel = new Label("Enter Filename: ");
        playButton = new Button("Play");
        stopButton = new Button("Stop");
  
        playButton.addActionListener(this);
        stopButton.addActionListener(this);
  
        // Add the label
        add(filenameLabel); 
  
        // Add the text field to enter filename
        add(filenameField); 
  
        // Add the "Play" button to play the music file
        add(playButton);    
  
         // Add the "Stop" button to stop playing
        add(stopButton);   
  
        // Initialize audioClip
        audioClip = null;
    }
  
    public void actionPerformed(ActionEvent e) {
        //if "Play" button is pressed
        if (e.getSource() == playButton) {
            // Store entered filename
            String filename = filenameField.getText(); 
            if (!filename.isEmpty()) {
                URL audioURL = getClass().getResource(filename);
                audioClip = getAudioClip(audioURL);
                //Play the audio (.wav)
                audioClip.play();
            }
        } else if (e.getSource() == stopButton) {
            //if "Stop" button is pressed
  
            if (audioClip != null) {
                //Stop the audio
                audioClip.stop();
            }
        }
    }
}
  
  
/*
<applet code="MusicPlayerApplet" height="250" width="300">
</applet>
*/


Executing the Program

1. Run the following command to compile the program.

javac MusicPlayerApplet.java

2. Run the following command to run the program.

appletviewer MusicPlayerApplet.java
execution

Execute the program

Steps to Run the Program

There are certain Steps mentioned below to run the Applet Program:

  1. Enter the filename with .wav extension.
  2. Click play button to play the audio.
  3. Click stop button to stop the audio.
    Output


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads