package net.woodyfolsom.msproj.sfx;

import java.io.IOException;
import java.io.InputStream;

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

public class SfxPlayer {
	private static final String clickFilename = "Click-SoundBible.com-1387633738.wav";
	private Clip clip;

	public SfxPlayer() throws LineUnavailableException, IOException, UnsupportedAudioFileException {
		init();
	}
	
	public void cleanup() {
		if (clip != null) {
			clip.close();
		}
	}
	
	public void init() throws LineUnavailableException, IOException, UnsupportedAudioFileException {
		if (clip != null) {
			clip.close();
		}
	    
		InputStream inputStream = SfxPlayer.class.getResourceAsStream(clickFilename);
		clip = AudioSystem.getClip();
	    clip.open(AudioSystem.getAudioInputStream(inputStream));
	}
	
	public void play() {
		if (clip!= null && !clip.isActive()) {
			clip.setFramePosition(0);
			clip.start();
		}
	}
}
