-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalarmClock.java
More file actions
101 lines (99 loc) · 3.58 KB
/
alarmClock.java
File metadata and controls
101 lines (99 loc) · 3.58 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.util.Scanner;
import java.time.format.DateTimeFormatter;
import java.time.LocalTime;
import java.time.format.DateTimeParseException;
import java.lang.Thread;
import java.io.*;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.File;
public class alarmClock {
public static void main(String[]args)
{
//java alarm clock
Scanner sc=new Scanner(System.in);
DateTimeFormatter formatter=DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime alarmtime=null;
String filepath="Nebula.wav";
while(alarmtime==null)
{
try {
System.out.print("Enter an alarm time (HH:MM:SS) : ");
String inTime = sc.nextLine();
alarmtime = LocalTime.parse(inTime, formatter); // for time set for future
System.out.println("Alarm set for " + alarmtime);
} catch (DateTimeParseException e) {
System.out.println("Invalid Format. Please use HH:MM:SS ");
}
}
Clock alarmclock=new Clock(alarmtime,filepath,sc);
Thread alarmthread=new Thread(alarmclock);
alarmthread.start();
//sc.close not used becoz thread class still needs it
}
}
class Clock implements Runnable{
private final String filepath;
private final LocalTime alarmtime;
private final Scanner sc; // to access scanner in playsound
Clock(LocalTime alarmtime,String filepath,Scanner sc)
{
this.alarmtime=alarmtime;
this.filepath=filepath;
this.sc=sc;
}
@Override
public void run()
{
LocalTime now = LocalTime.now(); //for present time
while(LocalTime.now().isBefore(alarmtime))
{
try{
Thread.sleep(1000);
int hours =now.getHour();
int minutes = now.getMinute();
int seconds =LocalTime.now().getSecond();//if used only now.get... the seconds change is not visible in o/p but the result is shown
// so used LocalTime.now().get.... to see the seconds changing continusly like timer in o/p
System.out.printf("\r%02d:%02d:%02d",hours,minutes,seconds);//used /r carriage return changes o/p on place rather than taking new line
}
catch(InterruptedException e)
{
System.out.println("Thread was interrupted");
}
}
System.out.println("\n*Alarm Noises*");
playSound(filepath);
}
private void playSound(String filepath)
{
File audiofile=new File(filepath);
try(AudioInputStream audioStream =AudioSystem.getAudioInputStream(audiofile)){
Clip clip =AudioSystem.getClip();
clip.open(audioStream);
clip.start();
System.out.println("Please *Enter* to stop alarm : ");
sc.nextLine();
clip.stop();
sc.close();
}
catch(UnsupportedAudioFileException e)
{
System.out.println("audio file format not supported!");
}
catch(LineUnavailableException e)
{
System.out.println("Audio is unavialable!");
}
catch(IOException e)
{
System.out.println("Error reading audio file!");
}
catch(IllegalStateException e)
{
System.out.println("audio interrupted!");
}
}
}