How to load audio file in Applet
Java JDK getAudioClip() API is used to play audio file. However, it’s supports only WAV, AIFF, AU, MIDI, and RMF file formats.
Tutorial
In this tutorial, you will learn to use JDK sound API to play audio file.
1. Create an Applet
Here’s an example to load audio file (“ding.wav”) in Applet. The code is self-explanatory
package com.mkyong.applet; import java.applet.Applet; import java.applet.AudioClip; import java.awt.Button; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class LoadSoundApplet extends Applet implements ActionListener { Button play, stop; AudioClip audioClip; private static final String PLAY = "PLAY"; private static final String STOP = "STOP"; public void init(){ play = new Button(); play.setLabel(PLAY); play.setActionCommand(PLAY); play.addActionListener(this); add(play); stop = new Button(); stop.setLabel(STOP); stop.setActionCommand(STOP); stop.addActionListener(this); add(stop); audioClip = getAudioClip(getCodeBase(), "ding.wav"); } @Override public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals(PLAY)){ audioClip.play(); }else if(e.getActionCommand().equals(STOP)){ audioClip.stop(); }else{ audioClip.stop(); } } }
2. Create a HTML
Create a HTML file to include the Applet.
<html> <head><title>Testing</title></head> <body> <h1>Applet Load Sound</h1> <applet width=300 height=100 code="com.mkyong.applet.LoadSoundApplet.class"> </applet> </body> </html>
3. Output
After you clicked on the Play button, Applet will start to play the “ding.wav”
What about MP3 file?
The JDK Sound API does not supports MP3 file, if you want to play MP3 in Applet, please read this article – How to play MP3 file in Applet


