-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoundTask.java
73 lines (66 loc) · 1.94 KB
/
SoundTask.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* Course: CSC421
* Assignment: #3
* Author: Andrew Seligman
* Date: May 21, 2014
* File: SoundTask.java
* Note: Code in this class for loading sounds is adapted from the
* tutorial located at:
* http://www3.ntu.edu.sg/home/ehchua/programming/java/J8c_PlayingSound.html
*******************************************************************************/
import java.util.*;
import java.io.*;
import javax.sound.sampled.*;
/**
* SoundTask.java plays a given sound after a specified period
* of time. The timer can be cancelled so that the sound will not play.
**/
public class SoundTask extends TimerTask
{
private Clip sound;
private AudioInputStream audioIn;
private Timer timer;
/**
* Constructor starts a timer for the specified time and loads the given
* sound to play when the timer completes.
*
* @param theTime is the specified time to wait before playing the sound
*
* @param file is the [path/]name of the specified sound to play relative
* to the location of the instantiating class
**/
public SoundTask(long theTime, String file)
{
super();
try
{
audioIn = AudioSystem.getAudioInputStream(
getClass().getResource(file));
sound = AudioSystem.getClip();
sound.open(audioIn);
}
catch(UnsupportedAudioFileException e){
e.printStackTrace();}
catch(IOException e){
e.printStackTrace();}
catch(LineUnavailableException e){
e.printStackTrace();}
timer = new Timer(true);
timer.schedule(this, theTime);
}
/**
* Play the sound when this task runs.
**/
@Override
public void run()
{
sound.start();
}
/**
* Cancel the timer so that the sound is not played.
**/
public void cancelSound()
{
timer.cancel();
}
}