Basic Steps:
// Initialize a SourceDataLine for playback
SourceDataLine line = AudioSystem.getSourceDataLine(audioFormat);
line.open(audioFormat);
line.start();
// Write the entire byte array to the line
line.write(byteArray, 0, byteArray.length);
line.drain();
line.close();
Converting integer or float arrays to a byte array:
The line.write() method expects a byte[], so your raw PCM amplitude values must be packed accordingly. The conversion depends on the audio format you choose. For 8‑bit signed PCM (what we’ll use in the example), a normalised float between -1 and 1 maps to a single byte with
(byte)(amplitude * 127). For 16‑bit signed, you’d need to write two bytes per sample in the correct endian order. The snippet below shows the simple 8‑bit mapping.