This snippet shows how to play an Audio Clip (midi, wav etc.) from an Applet.
The Audio Clip is loaded from the Applet CodeBase. The clip is loaded in Applet's init() method, then its started in the Applet's start() method and is stopped in the Applet's stop() method.
package com.livrona.snippets.applet;
import java.applet.AudioClip;
import java.awt.Graphics;
import javax.swing.JApplet;
/**
* This class plays an audio clip i.e a midi file in the applet. The clip is
* loaded with getAudioClip( ) in init( ), causing the applet to suspend until
* the download is completed. The sound is played repeatedly due to the loop( )
* call in start( ), continuing until the applet is removed from the browser
* (triggering a call to stop( )). If the page is displayed again, start( )'s
* call to loop( ) will play the music from the beginning.
*
* @author mvohra
*
*/
publicclass AudioClipPlayerApplet extends JApplet {
privatestaticfinallong serialVersionUID =6171200001504639474L;
private AudioClip mcdClip; // clip instance
publicvoid init() {
String clipFile ="test.mid";
// create the audio clip for that file
mcdClip = getAudioClip(getCodeBase(), clipFile);
}
/**
* Paint Method
*/
publicvoid paint(Graphics g) {
// draw String on the applet screen
g.drawString("Playing Sound ...", 25, 25);
}
/**
* Applet Stop method
*/
publicvoid stop() {
// stop the clip when done.
mcdClip.stop();
}
/**
* Applet Start method
*/
publicvoid start() {
// play the clip and loop over
mcdClip.loop();
}
} // end