FileDocCategorySizeDatePackage
AudioAnimator.javaAPI DocExample1223Sat Jun 02 03:09:52 BST 2001None

AudioAnimator.java

// 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.

import java.applet.*;
import java.awt.*;

public class AudioAnimator extends Animator {
    public void init() {
        // do Animator initialization first
        super.init();
        
        // look up the name of a sound, and then
        // load and play it (just once), in a separate thread.
        String soundname = this.getParameter("sound");
        new SoundPlayer(this, soundname);
    }
}

// This is the thread class that loads and plays the sound
class SoundPlayer extends Thread {
    private Applet applet;
    private String sound_url;
    // Store the information the run() method needs, and start it.
    public SoundPlayer(Applet app, String url) {
        applet = app;
        sound_url = url;
        this.start();
    }
    // This is the code the thread runs to load and play the sound
    public void run() { applet.play(applet.getDocumentBase(), sound_url); }
}