|
|
|
Know to play an Audio Clip in the Java
Applet
One of the most interesting features of Java is playing the
sound file. Let’s see how to play an audio clip in Java Applet
Viewer or within a web browser.
First create an applet Class named as “PlaySoundApplet”.
We have added two buttons to it, one is play button used to play
the sound in a loop and the other one is stop button used to
stop the sound. The play() method of AudioClip object is used
to play the sound and the stop() method is used to stop the
running audio clip, in between.
AudioClip class: Create an AudioClip class, which is an
abstract class that can’t be instantiated directly. Therefore we
have used getAudioClip() method to create an object of
AudioClip. This method is of Applet class. There are two
versions of getAudioClip() function:
1. public AudioClip getAudioClip(URL url)
2. public AudioClip getAudioClip(URL url, String
name)
The other method, which we have used here :
AudioClip = getAudioClip(getCodeBase(), “TestSnd.wav”)
This method has the following versions:
1. public abstract void play() – used to play the
sound only once.
2. public abstract void loop() – used to play the
sound in loop.
3. public abstract void stop() – used to stop the
playing sound.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class PlaySoundApplet extends Applet implements
ActionListener{
Button play,stop;
AudioClip audioClip;
public void init(){
play = new Button(“ Play in Loop “);
add(play);
play.addActionListener(this);
stop = new Button(“ Stop “);
add(stop);
stop.addActionListener(this);
audioClip = getAudioClip(getCodeBase(), “TestSnd.wav”);
}
public void actionPerformed(ActionEvent ae){ ]
Button source = (Button)ae.getSource();
if (source.getLabel() == “ Play in Loop “)
{
audioClip.play();
}
else if(source.getLabel() == “ Stop “){
audioClip.stop();
} }}
|
|
The HTML code is given below:
<HTML>
<BODY>
<APPLET CODE=”PlaySoundApplet” WIDTH=”200"
HEIGHT=”300">
</APPLET>
</BODY>
</HTML>
|
|
August 2007 | Java Jazz Up |36 |
|
|
|
Pages: 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 Download PDF |
|
|
|
|
|
|
|
|