-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
55 lines (46 loc) · 1.77 KB
/
Copy pathMain.java
File metadata and controls
55 lines (46 loc) · 1.77 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
import java.io.*;
import javax.sound.sampled.*;
public class Main {
public static void main(String[] args){
/* スキャナを開く */
java.util.Scanner sc = new java.util.Scanner(System.in);
System.out.println("停止する秒数を指示してください.");
int sleepSecond = sc.nextInt() * 1000;
try{
System.out.println("カウントを開始します。");
playSound("./start.wav");
Thread.sleep(sleepSecond);
/* wavの再生*/
System.out.println("そこまで。終了です。");
playSound("./sokomade.wav");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (UnsupportedAudioFileException | IOException | LineUnavailableException e){
e.printStackTrace();
}
}
public static void playSound(String filePath)
throws UnsupportedAudioFileException, IOException, LineUnavailableException
{
File audioFile = new File(filePath);
AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);
Clip clip = AudioSystem.getClip();
clip.open(audioStream);
clip.addLineListener(event -> {
if (event.getType() == LineEvent.Type.STOP) {
synchronized (clip) {
clip.notifyAll(); // 再生が終了したことを通知
}
}
});
clip.start(); // オーディオの再生を開始
synchronized (clip) {
try{
clip.wait(); // 再生が完了するまで待機
} catch (InterruptedException e){
e.printStackTrace();
}
}
clip.close(); // リソースを解放
}
}