5 // Created by Lutz Mueller on 8/13/07.
8 // Copyright (C) 2007 Lutz Mueller
10 // This program is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
15 // This program is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program. If not, see <http://www.gnu.org/licenses/>.
25 import java
.io
.IOException
;
27 import javax
.sound
.sampled
.AudioFormat
;
28 import javax
.sound
.sampled
.AudioInputStream
;
29 import javax
.sound
.sampled
.AudioSystem
;
30 import javax
.sound
.sampled
.DataLine
;
31 import javax
.sound
.sampled
.LineUnavailableException
;
32 import javax
.sound
.sampled
.SourceDataLine
;
34 public class SoundSystem
36 private static final int EXTERNAL_BUFFER_SIZE
= 64000;
38 public static void playSound(String filePath
)
40 File soundFile
= new File(filePath
);
41 AudioInputStream audioInputStream
= null;
43 try { audioInputStream
= AudioSystem
.getAudioInputStream(soundFile
); }
45 ErrorDialog
.show("play-sound","Unsupported audio file");
49 AudioFormat audioFormat
= audioInputStream
.getFormat();
51 SourceDataLine line
= null;
53 DataLine
.Info info
= new DataLine
.Info(SourceDataLine
.class, audioFormat
);
55 line
= (SourceDataLine
) AudioSystem
.getLine(info
);
56 line
.open(audioFormat
);
58 catch (LineUnavailableException e
) {
59 System
.out
.println("Sound data line not available");
63 ErrorDialog
.show("play-sound","Could nor open sound data line");
70 byte[] abData
= new byte[EXTERNAL_BUFFER_SIZE
];
72 while (nBytesRead
!= -1)
74 try { nBytesRead
= audioInputStream
.read(abData
, 0, abData
.length
); }
75 catch (IOException e
) {
76 ErrorDialog
.show("play-sound","Could not read audio file");
80 int nBytesWritten
= line
.write(abData
, 0, nBytesRead
);