I am recording some audio into a ByteArrayOutputStream with Java, but when I try to play that byte array I get a LineUnavailableException. This is my code that plays the audio:
public void playTone(ByteArrayOutputStream
try {
AudioFormat format = new AudioFormat(44100, 8, 2, true, true);
DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
byte audio[] = note.toByteArray();
InputStream input = new ByteArrayInputStream(audio);
TargetDataLine line1 = (TargetDataLine) AudioSystem.getLine(info);
...
} catch (LineUnavailableException e1) {
e1.printStackTrace();
}
}
This is the exception that I get:
javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 44100.0 Hz, 8 bit, stereo, 2 bytes/frame, not supported.
Now I have read in this question that the parameters of AudioFormat might be wrong, but in that question the user used 24 (bit) as second parameter, where only 8 and 16 are supported.
Does anyone know what I am doing wrong?
EDIT:
From the answer below I changed the following since I do not have a file but a ByteArrayOutputStream:
AudioFormat format = new AudioFormat(44100, 16, 2, true, true);
byte audio[] = note.toByteArray();
InputStream input = new ByteArrayInputStream(audio);
audioStream = new AudioInputStream(input, format, audio.length / format.getFrameSize());
// instead of
// audioStream = AudioSystem.getAudioInputStream(soundFile);