/*
 * Sound.java
 *
 * Created on 22 janvier 2005, 18:40
 */

package hyperion;

import java.applet.*;
import java.net.*;
import javax.sound.sampled.*;
import java.io.*;
/**
 *
 * @author CryoGen
 */
public class Sound implements LineListener{
   
    
    private Clip clip;

    private int index;
    /** Creates a new instance of Sound */
    public Sound(String son, Applet applet)
    {
        
        AudioInputStream ais = null;
        try {
            ais = AudioSystem.getAudioInputStream(new URL(son));
        }
        catch(Exception e) {
            System.out.println(e.getMessage());
        }
        if(ais != null) {
            AudioFormat	format = ais.getFormat();
            DataLine.Info info = new DataLine.Info(Clip.class, format);
            try {
                clip = (Clip) AudioSystem.getLine(info);
                clip.addLineListener(this);
                clip.open(ais);
            }
            catch(Exception e) {
                System.out.println(e.getMessage());
            }
        }
        else {
            System.out.println("hu-ho");
        }
    
        
        
    }
    
    /*
    public void play()
    {
        audios[index].play();
        index = (index+1) % 5;
    }*/
    
    public void update(LineEvent event) {
        if (event.getType().equals(LineEvent.Type.STOP)) {
            clip.close();
        }
        else if (event.getType().equals(LineEvent.Type.CLOSE)) {
            /*
             *	There is a bug in the jdk1.3/1.4.
             *	It prevents correct termination of the VM.
             *	So we have to exit ourselves.
             */
            System.exit(0);
        }
    }
    
    public void play() {
        clip.start();
    }
    
    public void stop() {
        clip.stop();
    }

    
    
    
}
