Hello forum , i have a problem. I must get audio from a file, print the sample and play the audio from the sample using the byte array.
I researched and arrived at this “solution” :
public static void test() throws IOException, UnsupportedAudioFileException, LineUnavailableException {
File myFile = new File("A:/Code/MeusProjetosJava/AudioAnn/src/resources/cat10.wav");
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(myFile);
DataInputStream dis = new DataInputStream(audioInputStream);
AudioFormat format = audioInputStream.getFormat();
byte[] sample = new byte[(int) (audioInputStream.getFrameLength() * format.getFrameSize())];
System.out.println("Sample.length = " + sample.length);
System.out.println("FrameLength :" + audioInputStream.getFrameLength());
System.out.println("Frame Size :" + format.getFrameSize());
System.out.println("SampleSizeInBits :" + format.getSampleSizeInBits());
System.out.println("Frame Rate : " + format.getFrameRate());
System.out.println("Sample Rate :" + format.getSampleRate());
System.out.println("Encoding :" + format.getEncoding());
System.out.println("Channels : " + format.getChannels());
dis.readFully(sample);
dis.close();
//WRONG WAY TO DO IT USING SUN.*
// Create the AudioData object from the byte array
AudioData audioData = new AudioData(sample);
// Create an AudioDataStream to play back
AudioDataStream audioStream = new AudioDataStream(audioData);
// Play the sound
AudioPlayer.player.start(audioStream);
}
The audio plays but with heavy distortion. Also i used the sun package,which i know i shouldnt.
So my question is, how do i play that byte array now?
Also, am i loading the byte array correctly? I dont want to lose quality because of the code.