-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlarm clock program
More file actions
126 lines (83 loc) · 3.06 KB
/
Alarm clock program
File metadata and controls
126 lines (83 loc) · 3.06 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Scanner;
public class main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
DateTimeFormatter formatter= DateTimeFormatter.ofPattern("HH:mm:ss");
LocalTime alarmTime=null;
String filePath="C:\\Users\\R O H I T H\\IdeaProjects\\my first project\\src\\alarm tone.wav";
while(alarmTime==null){
try{
System.out.print("Enter the time for the alarm(HH:MM:SS) : ");
String inputTime=scanner.nextLine();
alarmTime=LocalTime.parse(inputTime,formatter);
}
catch(DateTimeParseException e){
System.out.println("Invalid format");
}
AlarmClock alarmClock=new AlarmClock(alarmTime,filePath,scanner);
Thread alarmThread=new Thread(alarmClock);
alarmThread.start();
}
}
}
import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
import java.time.LocalTime;
import java.util.Scanner;
public class AlarmClock implements Runnable{
private final LocalTime alarmTime;
final private String filePath;
private final Scanner scanner;
AlarmClock(LocalTime alarmTime,String filePath,Scanner scanner){
this.alarmTime=alarmTime;
this.filePath=filePath;
this.scanner=scanner;
}
@Override
public void run(){
if (LocalTime.now().isBefore(alarmTime)){
while(LocalTime.now().isBefore(alarmTime)){
try{
Thread.sleep(1000);
LocalTime now=LocalTime.now();
System.out.printf("\r%02d:%02d:%02d",now.getHour(),now.getMinute(),now.getSecond());
}
catch(InterruptedException e) {
System.out.println("It has been Interrupted");
}
}
playSound(filePath);}
else{
System.out.println("The current time is already past the alarm time ");
System.out.println("Alarm will ring on time tomorrow");
}
}
public void playSound(String filePath){
try{
File file=new File(filePath);
AudioInputStream audioStream= AudioSystem.getAudioInputStream(file);
Clip clip=AudioSystem.getClip();
clip.open(audioStream);
clip.start();
System.out.println();
System.out.println("Alarm ringing");
System.out.print("press Enter to turn off the alarm");
scanner.nextLine();
clip.close();
scanner.close();
}
catch(UnsupportedAudioFileException e){
System.out.println("Unsupported audio file");
}
catch(LineUnavailableException e){
System.out.println("Line unavailable");
}
catch(IOException e){
System.out.println("Something went wrong");
}
}
}