When you use an applet to play a sound, you first define the AudioClip object, the getAudioClip method can give the sound to the AudioClip object, and if you just want to play the sound again, you should call the AudioClip class playing method, if you want to loop the sound clips, The loop method of the AudioClip class should be selected.
(1) Playing sound files
Image formats are various, such as BMP, GIF, and JPEG. As with sound files, wav and AU are the two most commonly used sound files. Currently Java support only au files, but the Windows environment is commonly used in WAV files, so it is best to have a WAV file to convert to the AU file tool.
* AudioClip class for playing sound
The AudioClip class is used to play sounds within Java applets, which are in Java. is defined in the applet package.
The following shows how to play a sound using the AudioClip class.
Mount a sound file named sample.au and Play (Sounddemo.java)
//源程序清单
import java.awt.*;
import java.applet.*
public class SoundDemo extends Applet
{
public void paint(Graphics g)
{
AudioClip audioClip=getAudioClip(getCodeBase(),”Sample.AU”);
//创建AudioClip对象并用//getAudioClip方法将其初始化。
g.drawstring("Sound Demo! ",5,15);
audioClip.loop();//使用AudioClip类的loop方法循环播放
}
}
The following HTML statement needs to be put 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, which will display an applet window and accompany the music. The music terminates when the applet is closed.