Play a sound file using Java applets

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.