// This example is from the book _Java in a Nutshell_ by David Flanagan. // Written by David Flanagan. Copyright (c) 1996 O'Reilly & Associates. // You may study, use, modify, and distribute this example for any purpose. // This example is provided WITHOUT WARRANTY either expressed or implied. package AudioAnimator; import java.applet.*; import java.awt.*; public class AudioAnimator extends Animator { protected Label label = new Label("Sound:"); protected TextField tx = new TextField("", 15); protected String soundname; public volatile SoundPlayer sdPlayer; public void init() { // do Animator initialization first super.init(); this.add(label); this.add(tx); // look up the name of a sound, and then // load and play it (just once), in a separate thread. soundname = this.getParameter("sound"); tx.setText(soundname); } public void start() { super.start(); sdPlayer = new SoundPlayer(this, soundname); sdPlayer.start(); } public void stop() { sdPlayer = null; super.stop(); } } // This is the thread class that loads and plays the sound class SoundPlayer extends Thread { private AudioAnimator applet; private AudioClip clip; // Store the information the run() method needs, and start it. public SoundPlayer(AudioAnimator app, String url) { applet = app; clip = app.getAudioClip(applet.getDocumentBase(), url); } // This is the code the thread runs to load and play the sound public void run() { applet.espera(5000); Thread thisThread = Thread.currentThread(); while ( applet.sdPlayer == thisThread ) { clip.loop(); applet.espera(5200); } clip.stop(); } }