/**
 * package contains classes for playing different kinds of sound files
 */
package simogl.effect.sound;

import java.applet.Applet;
import java.applet.AudioClip;
import java.io.File;
import java.net.MalformedURLException;

/**
 * This class represents a sampled file like .wav files, that can be
 * easily played via its play() method. It is not based on the Java 
 * Sound API, but simply uses the Applet-Soundplayer.
 * @author Frank Bruns
 *
 */
public class SampledSound
{
	private AudioClip audioClip = null;
	
	/**
	 * Initializes the sampled sound object with the sound file
	 * specified by the path parameter. 
	 * @param path path to a sound file
	 */
	public SampledSound(String path)
	{
		this.setSound(path);
	}
	
	/**
	 * Creates a new ampled sound object without providing an 
	 * initial sound file.
	 *
	 */
	public SampledSound(){}
	
	/**
	 * Assignes a sound file to the sampled sound object
	 * @param path path to the sound file
	 */
	public void setSound(String path)
	{
		try
        {
        	this.audioClip = Applet.newAudioClip(new File(path).toURL());
        }
        catch(MalformedURLException e) 
        {
        	System.out.println("Problem with Loading sound file from " +
        			"path: "+path);
        };
	}
	
	/**
	 * Plays the audio file currently assigned to the sampled sound object
	 * @param loop play sound in loop? --> true = yes, false = no
	 */
	public void play(boolean loop)
	{
		if(this.audioClip != null)
		{			
			if(loop == false) audioClip.play();
			if(loop == true) audioClip.loop();		
		}
		else System.out.println("Couldn't play sound file. No sound file " +
				"was assigned to the sound player object");		
	}

	/**
	 * Stops the currently playing sound file
	 *
	 */
	public void stop()
	{
		if(this.audioClip != null)
		{
			this.audioClip.stop();
		}
		else System.out.println("Couldn't stop sound file. No sound file " +
				"was assigned to the sampled sound object");		
	}
}
