//  File:  AudioItem\AudioItem.java

//  This version implements MouseListener directly

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.net.URL;

public class AudioItem extends Applet implements MouseListener {

    /**
     * Play the next sound. The sound URLS are obtained
     * from the "snd" attribute. You can specify a list
     * of them by separating the sounds 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) {
                audio.stop();
                audio = null;
            }
        
            url = sounds;
            if (sounds.indexOf('|') >= 0) {
                int start = index;
                if ((index = sounds.indexOf('|', index)) < 0) {
                    url = sounds.substring(start);
                    index = 0;
                } else {
                    url = sounds.substring(start, index++);
                }
            }
            if (url.length() > 0) {
                // If you want to force users to play ONLY with a 
                // Java2-enabled browser, then change getAudioClip
                // to newAudioClip in the next line of code
                audio = getAudioClip(new URL(getDocumentBase(), url));
                audio.play();
                repaint();
                ++soundNum;
            }
        } catch (Exception e) {
        }
    }

    /**
    *    Initialize the applet; acquire the "sounds" parameter value"
    */
    public void init() {
        addMouseListener(this);
        setSize(400, 250);
        sounds = getParameter("snd");
        if (sounds == null) {
            sounds = "Audio/beep.au";
        }
    }

    /**
    *     Start playing sounds; get the next one...
    */
    public void start() {
        next();
    }

    /**
    *     Stop playing sounds
    */
    public void stop() {
        if (audio != null) {
            audio.stop();
            audio = null;
        }
    }

    private String sounds, url;
    private int index, soundNum;
    private AudioClip audio;
    
    /**
        MouseListener methods - we would have extended MouseAdapter,
        but we already are extending Applet...  so we can't
    */
    public void mouseClicked(MouseEvent e) { }

    public void mousePressed(MouseEvent e) { }

    public void mouseReleased(MouseEvent e) {
        e.consume();
        next();     // When the user clicks in the applet,
    }               // play the next sound

    public void mouseEntered(MouseEvent e) { }

    public void mouseExited(MouseEvent e) { }

    /**
    *     Paint the screen -- the triangle
    */
    public void paint(Graphics g) {
        double f = ((double)(getSize().height - 1)) / ((getSize().width - 1) * 2);
        int offset = getSize().height / 2;
        for (int i = getSize().width - 1; i >= 0; i -= 3) {
            int h = (int)(i * f);
            g.drawLine(i, offset - h, i, offset + h);
        }
        g.drawString("Click anywhere in the Applet to play the next sound", 10, 20);
        g.drawString("Sound #" + soundNum + "   " + url.trim(), 10, 40);
        if (index <= 0)
            soundNum = 0;
    }

}
