OSDN Git Service

0b6cb0e96595baf509bd437d1abf7fa5aa8e397d
[hayashilib/hayashi.git] / src / hayashi / yuu / tools / sound / PlaySound.java
1 package hayashi.yuu.tools.sound;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 import javax.sound.sampled.AudioFormat;
7 import javax.sound.sampled.AudioInputStream;
8 import javax.sound.sampled.AudioSystem;
9 import javax.sound.sampled.DataLine;
10 import javax.sound.sampled.LineUnavailableException;
11 import javax.sound.sampled.SourceDataLine;
12 import javax.sound.sampled.UnsupportedAudioFileException;
13
14 /**
15  * サウンドファイルを再生して、音(音楽)を鳴らす。
16  *
17  * @author hayashi
18  *
19  */
20 public class PlaySound extends Thread
21 {
22         //static final String WAVE = "lib/belltree.aiff";
23         static String WAVE = null;
24
25         /**
26          * サウンドファイルを再生して、音(音楽)を鳴らす。
27          * (1) PlaySound(sound file);
28          *                      再生するサウンドファイルを指定して、インスタンスを生成する。
29          *                      サウンドファイルを指定しなければ、「BEEP」音が再生される。
30          * (2) playSound.start();
31          *                      サウンド再生を実行。
32          *
33          * @param args  args
34          */
35         public static void main(String[] args) {
36                 /*
37                  * BEEP音を再生
38                  */
39                 try {
40                         PlaySound obj = new PlaySound(null);
41                         for (int i=0; i < 5; i++) {
42                                 try {
43                                         obj.start();
44                                 } catch (IllegalThreadStateException e) {}
45                                 try {
46                                         Thread.sleep(1000);     // 1秒間停止
47                                 } catch (InterruptedException e) {}
48                         }
49                 }
50                 catch (Exception e) {
51                         e.printStackTrace();
52                 }
53
54                 /*
55                  * サウンドファイルを再生
56                  */
57                 try {
58                         PlaySound obj = new PlaySound("lib/belltree.aiff");
59                         for (int i=0; i < 5; i++) {
60                                 try {
61                                         obj.start();
62                                 } catch (IllegalThreadStateException e) {}
63                                 try {
64                                         Thread.sleep(1000);     // 1秒間停止
65                                 } catch (InterruptedException e) {}
66                         }
67                 }
68                 catch (Exception e) {
69                         e.printStackTrace();
70                 }
71         }
72
73         private static final int EXTERNAL_BUFFER_SIZE = 128000;
74         private static AudioInputStream audioInputStream = null;
75         private static AudioFormat audioFormat = null;
76
77         /**
78          * コンストラクタ
79          * @param path  パス
80          * @throws IOException          例外
81          * @throws UnsupportedAudioFileException        例外
82          * @throws LineUnavailableException                     例外
83          */
84         public PlaySound(String path) throws IOException, UnsupportedAudioFileException, LineUnavailableException {
85                 super();
86                 PlaySound.WAVE = path;
87                 if (PlaySound.WAVE != null) {
88                     File soundFile = new File(PlaySound.WAVE);
89                     PlaySound.audioInputStream = AudioSystem.getAudioInputStream(soundFile);
90                     PlaySound.audioFormat = PlaySound.audioInputStream.getFormat();             // オーディオ形式
91                 }
92         }
93
94         @Override
95         public void run() {
96                 if (PlaySound.WAVE == null) {
97                         java.awt.Toolkit.getDefaultToolkit().beep();
98                 }
99                 else {
100                         try {
101                                 // データラインの情報オブジェクトを生成
102                                 DataLine.Info info = new DataLine.Info(SourceDataLine.class, PlaySound.audioFormat);
103
104                                 // 指定されたデータライン情報に一致するラインを取得します
105                                 SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
106
107                                 // 指定されたオーディオ形式でラインを開きます
108                                 line.open(PlaySound.audioFormat);
109
110                                 // ラインでのデータ入出力を可能にします
111                                 line.start();
112
113                                 int nBytesRead = 0;
114                                 byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
115                                 while (nBytesRead != -1) {
116                                         // オーディオストリームからデータを読み込みます
117                                         nBytesRead = PlaySound.audioInputStream.read(abData, 0, abData.length);
118                                         if (nBytesRead >= 0) {
119                                                 // オーディオデータをミキサーに書き込みます
120                                                 line.write(abData, 0, nBytesRead);
121                                         }
122                                 }
123
124                                 // ラインからキューに入っているデータを排出します
125                                 line.drain();
126                                 line.close();
127                         }
128                         catch (Exception e) {
129                                 e.printStackTrace();
130                         }
131                 }
132         }
133 }