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.