我已经制作了倒计时钟,但是我想让它播放倒计时结束的声音。我的问题是我得到一个NullPointerException,我不知道如何解决它。这是没有导入的代码:
public class ReminderBeep {
Toolkit toolkit;
private String Path_to_sound = "sirenpolice5.wav";
public AudioClip audioclip;
Timer timer;
public ReminderBeep(int seconds) {
toolkit = Toolkit.getDefaultToolkit();
timer = new Timer();
timer.schedule(new RemindTask(), seconds * 1000);
}
class RemindTask extends TimerTask {
public void run() {
System.out.println("Time's up!");
try {
URL url = this.getClass().getClassLoader().getResource(Path_to_sound);
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
} catch (MalformedURLException murle) {
System.out.println(murle);
} catch (LineUnavailableException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.exit(0);
}
}
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
System.out.println("Welcome to countdown clock. Please type how many seconds you wanna count down from");
int sec = scan.nextInt();
new ReminderBeep(sec);
System.out.println("Counting");
}
}
答案 0 :(得分:0)
找到了我错过的问题Thread.sleep(clip.getMicrosecondLength() / 1000);