64 lines
		
	
	
	
		
			1.3 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			64 lines
		
	
	
	
		
			1.3 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
import java.io.*;
 | 
						|
import java.net.URL;
 | 
						|
import java.util.Timer;
 | 
						|
import java.util.TimerTask;
 | 
						|
import javax.sound.sampled.*;
 | 
						|
 | 
						|
public class PlaySound {
 | 
						|
  private Timer timer;
 | 
						|
 | 
						|
  private boolean playing;
 | 
						|
 | 
						|
  private final String RES_TICK = "432.wav";
 | 
						|
  private final String RES_WIN = "433.wav";
 | 
						|
  private final String RES_BOOM = "434.wav";
 | 
						|
 | 
						|
  public PlaySound() {
 | 
						|
    this.playing = false;
 | 
						|
  }
 | 
						|
 | 
						|
  public boolean isPlaying() {
 | 
						|
    return this.playing;
 | 
						|
  }
 | 
						|
 | 
						|
  public void play(String filename) {
 | 
						|
    URL f = getClass().getResource(filename);
 | 
						|
    try {
 | 
						|
      AudioInputStream in = AudioSystem.getAudioInputStream(f);
 | 
						|
      Clip clip = AudioSystem.getClip();
 | 
						|
      clip.open(in);
 | 
						|
      clip.start();
 | 
						|
    } catch (UnsupportedAudioFileException e) {
 | 
						|
    } catch (IOException e) {
 | 
						|
    } catch (LineUnavailableException e) {
 | 
						|
    }
 | 
						|
  }
 | 
						|
 | 
						|
  public void tick() {
 | 
						|
    this.playing = true;
 | 
						|
    this.timer = new Timer();
 | 
						|
    TimerTask tt =
 | 
						|
        new TimerTask() {
 | 
						|
          @Override
 | 
						|
          public void run() {
 | 
						|
            play(RES_TICK);
 | 
						|
          }
 | 
						|
        };
 | 
						|
    this.timer.scheduleAtFixedRate(tt, 0, 1000);
 | 
						|
  }
 | 
						|
 | 
						|
  public void win() {
 | 
						|
    this.play(this.RES_WIN);
 | 
						|
  }
 | 
						|
 | 
						|
  public void boom() {
 | 
						|
    this.play(this.RES_BOOM);
 | 
						|
  }
 | 
						|
 | 
						|
  public void stop() {
 | 
						|
    if (this.playing) {
 | 
						|
      this.timer.cancel();
 | 
						|
      this.playing = false;
 | 
						|
    }
 | 
						|
  }
 | 
						|
}
 |