When using Applet to play sound, you need to first define the AudioClip object. The GetAudioClip method can assign the sound to the AudioClip object. If you only want to play the sound once, you should call the play method of the AudioClip class. If you want to loop the sound clip, you should use the loop method of the AudioClip class. .
(1) Play sound files and
image formats in various formats, such as BMP, GIF and JPEG, etc. The same goes for sound files. WAV and AU are the two most commonly used sound files. Currently, Java only supports AU files, but WAV files are commonly used in Windows environments, so it is best to have a tool that can convert WAV files into AU files.
* AudioClip class for playing sounds
The AudioClip class is used to play sounds in Java Applet. This class is defined in the java.Applet package.
The following demonstrates how to use the AudioClip class to play sounds.
Load a sound file named Sample.Au and play it (SoundDemo.java)
//Source program list
import java.awt.*;
import java.applet.*
public class SoundDemo extends Applet
{
public void paint(Graphics g)
{
AudioClip audioClip=getAudioClip(getCodeBase(),"Sample.AU");
//Create an AudioClip object and initialize it with //getAudioClip method.
g.drawstring("Sound Demo! ",5,15);
audioClip.loop(); //Use the loop method of the AudioClip class to loop playback}
}
You need to put the following HTML statements into the SoundDemo.HTML file to prepare for running the Applet.
<HTML>
<TITLE>SoundDemo Applet</TITLE>
<APPLET CODE="SoundDemo.class" WIDTH=300 HEIGHT=200>
</APPLET>
</HTML>
Compile and run the Applet, an Applet window will be displayed on the screen accompanied by music. The music ends when the Applet is closed.