4 import java
.io
.IOException
;
5 import javax
.sound
.sampled
.AudioFormat
;
6 import javax
.sound
.sampled
.AudioInputStream
;
7 import javax
.sound
.sampled
.AudioSystem
;
8 import javax
.sound
.sampled
.DataLine
;
9 import javax
.sound
.sampled
.FloatControl
;
10 import javax
.sound
.sampled
.LineUnavailableException
;
11 import javax
.sound
.sampled
.SourceDataLine
;
12 import javax
.sound
.sampled
.UnsupportedAudioFileException
;
14 public class AePlayWave
extends Thread
{
16 private String filename
;
18 private Position curPosition
;
20 private final int EXTERNAL_BUFFER_SIZE
= 524288; // 128Kb
26 public AePlayWave(String wavfile
) {
28 curPosition
= Position
.NORMAL
;
31 public AePlayWave(String wavfile
, Position p
) {
38 File soundFile
= new File(filename
);
39 if (!soundFile
.exists()) {
40 System
.err
.println("Wave file not found: " + filename
);
44 AudioInputStream audioInputStream
= null;
46 audioInputStream
= AudioSystem
.getAudioInputStream(soundFile
);
47 } catch (UnsupportedAudioFileException e1
) {
50 } catch (IOException e1
) {
55 AudioFormat format
= audioInputStream
.getFormat();
56 SourceDataLine auline
= null;
57 DataLine
.Info info
= new DataLine
.Info(SourceDataLine
.class, format
);
60 auline
= (SourceDataLine
) AudioSystem
.getLine(info
);
62 } catch (LineUnavailableException e
) {
65 } catch (Exception e
) {
70 if (auline
.isControlSupported(FloatControl
.Type
.PAN
)) {
71 FloatControl pan
= (FloatControl
) auline
72 .getControl(FloatControl
.Type
.PAN
);
73 if (curPosition
== Position
.RIGHT
)
75 else if (curPosition
== Position
.LEFT
)
81 byte[] abData
= new byte[EXTERNAL_BUFFER_SIZE
];
84 while (nBytesRead
!= -1) {
85 nBytesRead
= audioInputStream
.read(abData
, 0, abData
.length
);
87 auline
.write(abData
, 0, nBytesRead
);
89 } catch (IOException e
) {