在Java中播放警报

时间:2018-10-21 09:11:19

标签: java timer alarm

我正在尝试在闹钟响起时播放声音片段,但是问题是我要求用户输入的闹钟时间是整数,并且我正在通过当前时间在屏幕上显示时钟系统的。因此,系统会将系统的当前时间保存为变量,并每分钟更改一次。我想做的是,当我将当前时间保存为分钟和小时时,它会将其与控制台值进行比较,即按我自己的意愿而不是系统自己的时间递增:

import java.util.Scanner;
import java.io.File; 
import java.io.IOException; 
import java.util.Scanner; 
import java.util.Calendar;
import javax.sound.sampled.AudioInputStream; 
import javax.sound.sampled.AudioSystem; 
import javax.sound.sampled.Clip; 
import javax.sound.sampled.LineUnavailableException; 
import javax.sound.sampled.UnsupportedAudioFileException; 

import java.time.LocalDateTime;
// java new implentation of (see tester code) much better
public class main 
{
static Scanner scanner = new Scanner(System.in);
public static  int h,m;

public static void main(String a[]) throws InterruptedException,UnsupportedAudioFileException,IOException,LineUnavailableException 
{
    // init 
    Calendar cal = Calendar.getInstance();
    int hour = cal.get(Calendar.HOUR_OF_DAY);
    int minute = cal.get(Calendar.MINUTE);


    //clockdisplay simply keeps it in 12 hour format
    ClockDisplay clock = new ClockDisplay(hour,minute );

    System.out.println("Set Alarm!!");

    System.out.println("Please enter hours!! ");
    h=scanner.nextInt();
    System.out.println("Please enter minutes!!");
    m = scanner.nextInt();
    do{
        System.out.print("\r" + clock.getTime());
        // -=-=-=-= waqfa -=--=-=-=
        Thread.sleep(1000);
        // -=-=-=-= waqfa ends -=-=
        // forward
        if(LocalDateTime.now().getHour()==h && LocalDateTime.now().getMinute() == m)
        {
            SimpleAudioPlayer audioPlayer = new SimpleAudioPlayer();
            audioPlayer.play(); 
            System.out.println("Alarm ringing");
            break;
        }
        clock.timeTick();
    }while(true); // do while ends
}
}

0 个答案:

没有答案