AudioClip in java applet: Load and Playing Sound with Examples
Description:
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 meow.au 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:
1 |
play (getCodeBase (), "audio / xyz.au"); |
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:
1 |
AudioClip clip = getAudioClip (getCodeBase (),"audio / loop.wav"); |
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:
1 |
AudioClip clip = newAudioClip ("audio / loop.wav"); |
To play the clip once, use the play() method:
1 |
clip.play(); |
To stop a currently playing sound clip, use the stop() method:
1 |
clip.stop(); |
To loop the clip (play it repeatedly) use the loop() method:
1 |
clip.loop(); |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public void stop() { if (runner != null) { if (bgsound != null) bgsound.stop(); runner.stop(); runner = null; } } |
above example shows a simple basic structure for an applet that plays two sounds: the first, one Background sound called loop.au is played repeatedly. The second, a beep (beep.au), is all played for five seconds.
Example: load and Playing the Sound using AudioClip method in java applet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
import java.awt. *; import java.applet.*; public class Testing extends Applet implements Runnable{ AudioClip bgSound; AudioClip beep; Thread runner; public void start(){ if(runner == null) { runner = new Thread(this); runner.start(); } } public void stop() { if (runner != null) { if (bgSound != null) bgSound.stop(); runner = null; } } public void init() { bgSound = getAudioClip(getCodeBase(),"simple.AU"); beep = getAudioClip(getCodeBase(), "simple.AU"); } public void run() { if (bgSound != null) bgSound.loop(); Thread thisThread = Thread.currentThread(); while (runner == thisThread) { try { Thread.sleep(5000); } catch (InterruptedException e) { } if (beep != null) beep.play(); } } public void paint(Graphics screen) { screen.drawString("Playing Sounds ...", 10, 10); } } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import java.awt. *; import java.applet.*; import javax.swing. *; import java.awt.event.*; public class Testing extends Applet implements ActionListener{ AudioClip sound; public void init() { sound = getAudioClip(getCodeBase(),"simple.au"); Button play =new Button("Play"); add(play); Button stop =new Button("Stop"); add(stop); play.addActionListener(this); stop.addActionListener(this); } public void actionPerformed(ActionEvent e) { String s = e.getActionCommand(); if("Play".equals(s)) sound.play(); if("Stop" .equals(s)) sound.stop(); } } |
Html Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<html> <head> <title> Play sound Example </title> </head> <body> <Applet code="Testing.class" width=500 height =500> </Applet> </body> </html> |
Output:
Output in web browser:
Programming Explanation:
1 2 3 4 5 6 7 |
import java.awt. *; import java.applet.*; import javax.swing. *; import java.awt.event.*; |
First of all I import the java awt package, applet package, event, swing package.
1 |
public class Testing extends Applet implements ActionListener |
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.
1 |
AudioClip sound; |
Then I create the instance for audio clip AudioClip sound. this should be specified a scope global values before any of the methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public void init() { sound = getAudioClip(getCodeBase(),"simple.au"); Button play =new Button("Play"); add(play); Button stop =new Button("Stop"); add(stop); play.addActionListener(this); stop.addActionListener(this); } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public void actionPerformed(ActionEvent e) { String s = e.getActionCommand(); if("Play".equals(s)) sound.play(); if("Stop" .equals(s)) sound.stop(); } |
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.
Thanks -:)