//  File:  SimpleSounds\SimpleSound.sjava

//  A Simple Sound-Playing Applet

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.URL;

public class SimpleSounds extends Applet {
    /**
     * Play the next sound.  The sound URLS are obtained
     * from the "snd" attribute.  You can specify a list
     * of them by separating the aSound by '|'s. <p>
     * Note that the URL is constructed relative to the
     * getDocumentBase(), that is because the url is obtained
     * from within the document
     */
    public void next() {
        try {
            if (audio != null) {    // stop playing the current sound, if any...
                audio.stop();
                audio = null;
            }
        
            url = sounds[soundNum]; // get the pathname for the next sound
            audio = getAudioClip(new URL(getDocumentBase(), url));
            audio.play();
            repaint();
            soundNum = ((soundNum + 1) % sounds.length);
        } catch (Exception e) {
            // ignore any exception...
        }
    }

    /**
    *   Initialize the applet; listen for mouseReleased
    */
    public void init() {
        addMouseListener(new MyMouseListener());
        setSize(300, 50);
        aSound = sounds[0];
    }

    /**
    *   Start playing aSound; go to the next one
    */
    public void start() {
        next();
    }

    /**
    *   Stop playing sound
    */
    public void stop() {
        if (audio != null) {
            audio.stop();
            audio = null;
        }
    }

    /**
    *   Paint the screen -- put up info about the current sound
    */
    public void paint(Graphics g) {
        g.drawString("Click anywhere in the Applet to play the next sound", 10, 20);
        g.drawString("Sound #" + soundNum + "   " + url.trim(), 10, 40);
    }
    
    /**
    *   Handle mouseReleased -- i.e., play the next sound
    */
    private class MyMouseListener extends MouseAdapter {
        public void mouseReleased(MouseEvent e) {
            next();     // When the user clicks in the applet, play the next sound
           }
    }

    private String aSound, url;
    private int soundNum;
    private AudioClip audio;
    private String[] sounds = { "bark.au", "gong.au", "bang_cucoo.au" };
}
