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>
*/


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

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

Similar Reads

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....

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:...

Executing the Program

...

Steps to Run the Program

1. Run the following command to compile the program....

Contact Us