|
Description:
|
Sound can be played in an applet by using java.applet.AudioClip Interface. The applet method getAudioClip(Url, String) returns the AudioClip object. The sound can be played in two ways, by calling play() method of the AudioClip to play once, and by calling loop() method to play it repeatedly in a loop. It is very important to override the stop() method of an applet if you are playing the sounds, and call stop() method of the AudioClip, to ensure that the sound is played only while the applet's web page is viewed. Otherwise the sound will continue playing even if the user switches to another web page.
To play sound in an applet only one file format is supported, and this format is .au file.
|
|
|
Example Code:
|
This example is an applet playing sound, the buttons; Play, Stop, Loop explains how to play sound once and repeatedly in a loop. The sound file name is passed as a parameter to the applet.
|
/*
* This example is from javareference.com
* for more information visit,
* http://www.javareference.com
*/
//package
//import statements
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/**
* ParamAppet .java
* class description
*/
public class PlaySoundApplet extends Applet implements ActionListener,
ItemListener
{
//static variables
//private variables
private AudioClip m_audio;
private Label m_statusLabel;
private Button m_play;
private Button m_stop;
private Checkbox m_loopCheck;
//public variables
/**
* init()
* This is the first method called by the browsers JVM, when the
* applet is instantiated.
* This method can be used to create the applets GUI elements
*
* @return void
* @exception
*/
public void init()
{
//setting the applet Layout
setLayout( new BorderLayout());
Panel controls = new Panel(new FlowLayout());
m_statusLabel = new Label("Sound not playing...", Label.CENTER);
m_statusLabel.setFont(new Font("Arial", Font.BOLD,14));
m_play = new Button("Play >");
m_stop = new Button("Stop ||");
m_loopCheck = new Checkbox("Loop", false);
m_play.addActionListener(this);
m_stop.addActionListener(this);
m_loopCheck.addItemListener(this);
m_audio = getAudioClip(getDocumentBase(),
getParameter("SoundFileName"));
controls.add(m_loopCheck);
controls.add(m_play );
controls.add(m_stop );
add(BorderLayout.NORTH, m_statusLabel);
add(BorderLayout.CENTER, controls);
}
/**
* stop
* Overriding stop method to stop the sound
* when the user leaves applet's web pag
*
* @return void
* @exception
*/
public void stop()
{
m_audio.stop();
super.stop();
}
/**
* actionPerformed
* Handling Action Events
*
* @param ActionEvent
* @return void
* @exception
*/
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource().equals(m_play))
{
if(m_loopCheck.getState())
{
m_audio.loop();
m_statusLabel.setText("Sound Playing in Loop...");
}
else
{
m_audio.play();
m_statusLabel.setText("Sound Playing, will stop after some time...");
}
}
else if(ae.getSource().equals(m_stop))
{
m_audio.stop();
m_statusLabel.setText("Sound Stopped...");
}
}
/**
* itemStateChanged()
* Handling Item Event
*
* @param ItemEvent
* @return void
* @exception
*/
public void itemStateChanged(ItemEvent ie)
{
if(ie.getSource().equals(m_loopCheck))
{
if(m_loopCheck.getState())
{
m_audio.stop();
m_audio.loop();
m_statusLabel.setText("Sound Playing in Loop...");
}
else
{
m_audio.stop();
m_audio.play();
m_statusLabel.setText("Sound Playing, will stop after some time...");
}
}
}
}
|
|
Demonstration:
|
The HTML code for the above applet is as follows,
< HTML >
< TITLE >
Playing Sound in an Applet, Demonstration
< /TITLE >
< BODY >
< APPLET CODE="PlaySoundApplet.class" WIDTH=400 HEIGHT=175 >
< PARAM NAME=SoundFileName VALUE="spacemusic.au" >
< /APPLET >
< /BODY >
< /HTML >
Here is how the above applet will be executed.
|
|