AudioClip in java applet: Load and Playing Sound with Examples


AudioClip in java applet: Load and Playing Sound: in this article, I am going to show you how to use the Audioclip method to load and playing sound in the java applet with programming explanation.

Load and Playing sounds(AudioClip):

Java offers predefined support for playing sounds in connection with the sequence of animations or for independent playback. As well as the support for images is located there is also support for sounds in the applet and AWT classes. The use of sounds is just as easy as loading and using images.

Before Java 1.2, only one sound format was supported: 8 kHz mono AU with Sun mu-law encoding. AU files are smaller than other sound files in other formats, but the sound quality is not especially good. If you wanted to use sounds that were available in other formats, you had to convert them into the Convert AU format, which was often associated with a loss of quality.

Java 1.2 offers much more extensive audio support. You can get digitized sounds of the Load and play the following formats: AIFF, AU and WAF. In addition, there are three formats MIDI base supports: Type 0 MIDI, Type 1 MIDI and RMF. The greatly expanded support of sounds can handle audio data in eight and 16 bit, mono or stereo, and sampling rates from 8 kHz to 48 kHz bypass.

The easiest way to load and play a sound is to use the play() method. This forms part of the applet class and is therefore available to you in applets. The play() method is that getImage() method very similar. It can also be used in the following two forms:

  • play() with one argument, a url object, loads and plays the one specified at that url Audio clip.
  • play() with two arguments, a base URL and a path, loads and plays this audio file. The first argument is sensibly a call to getDocumentBase() or getCodeBase().

For example, the following line of code loads the file and plays the sound it contains. The file is located in the audio directory, which in turn is placed in the same directory as the applet is:

The play() method loads the sound file and plays the sound as soon as possible after the call is made is. If the sound is not found, no error message appears, the sound is just simply not to listen. If you want to play a specific sound repeatedly, start and stop the sound file or execute them as a loop (to play them over and over again). In this case, use the Applet method getAudioClip() to convert the sound file into an instance of the AudioClip class (part of java.applet). Don’t forget to import them. You can then directly with work on this AudioClip object in java applet.

Suppose you have created a sound loop that runs permanently in the background of the applet shall be. You can use the following line in the initialization code for such a sound file:

The getAudioClip () method can only be called in an applet. Under Java 1.2 you can Applications load sound files using the newAudioClip () method of the Applet class. Afterwards the previous example is rewritten for use in an application:

To play the clip once, use the play() method:

To stop a currently playing sound clip, use the stop() method:

To loop the clip (play it repeatedly) use the loop() method:

If the method getAudioClip() or newAudioClip () does not find the specified sound or it turns off fails to load for some reason, returns null. It makes sense to write the code for this case test before attempting to play the sound file, as an attempted call to play(), stop(), and loop() methods on a null object result in an error (one exception). Any number of sound files can be played in an applet; all sounds become exactly the same with each other mixed as they are played in the applet.

Note that when using background sounds with repeating loops, the sound file is not automatically paused when the applet’s thread is stopped. That is, if a reader is too changes to another page, the sound of the first applet will continue to play. You can do this Solve the problem by stopping the applet’s background sound with the stop() method:

above example shows a simple basic structure for an applet that plays two sounds: the first, one Background sound called is played repeatedly. The second, a beep (, is all played for five seconds.

Example: load and Playing the Sound using AudioClip method in java applet:

Compile and run:



Output in web browser:


In this example, You have learned to use sounds that you can incorporate into your applets whenever you want them need – in certain situations or as a background sound that is played repeatedly for as long as the applet is running. You learned how to use both the play() method and the, load and play getAudioClip().

Example How to play and stop sound with buttons in java applet:

Java Code:

Html Code:



Output in web browser:


Programming Explanation:

First of all I import the  java awt package, applet package, event, swing package.

here this Testing extends applet and implements action listener because action listener is an interface that supports event model so event generation and event handling is implemented using action listener interface.

Then I create the instance for audio clip AudioClip sound.  this should be specified a scope global values before any of the methods.

Inside init method I assign the path of the audio clip to the instance of AudioClip sound. Then I create play object for button class with caption Play. Then I added the button on the panel with the help of add method.

Repeats the same steps for  stop button.

Then i registered that buttons with the event model.

To play the actual audio file for that we will be implementing action perform method the argument or parameter for actionperform is ActionEvent.

then I assign a string variable  for actionperformed command.  So getactioncommand will give you the caption of that button in my case my button caption is Play.

Then I write the if condition to perform the action. The condition is very simple if the caption is equal to the clicked button caption which is stored in the string variable, then performed some action.

Repeats same steps for stop button.

Related Articles

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button