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
| AudioInputStream audioInputStream = null;
try {
AudioInputStream originalAudioInputStream = AudioSystem.getAudioInputStream(waveFile);
AudioFormat originalFormat = originalAudioInputStream.getFormat();
AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
originalFormat.getSampleRate(),
originalFormat.getSampleSizeInBits(),
originalFormat.getChannels(),
originalFormat.getFrameSize(),
originalFormat.getFrameRate(),
originalFormat.isBigEndian()
);
audioInputStream = AudioSystem.getAudioInputStream(decodedFormat, originalAudioInputStream);
byte[] soundByte = new byte[2];
int[] soundInt = new int[882];
int index = 0;
while (audioInputStream.read(soundByte, 0, soundByte.length) != -1) {
soundInt[index] = ((soundByte[1] << 8) | soundByte[0]);
if (index == (soundInt.length - 1)) {
index = 0;
speaker.write(soundInt);
Utils.wait(20);
} else {
index++;
}
}
} catch(Exception e) {
e.printStackTrace();
} finally {
if (audioInputStream != null) {
try {
audioInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} |
Partager