[SOLVED]Play Audio from Byte Array without distortion

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.

I had good experience with this one:
http://www.javazoom.net/javalayer/javalayer.html

maybe look at the tutorials, they explain how to use SourceDataLine to communicate
with the system’s mixer…

Is there a class assignment requirement to put the data in a byte array? If not, you can use the AudioInputStream to open a Clip object. Playing back a Clip is one of two standard methods for playback supported by Java audio. It is optimized for sounds that are short enough that they can be preloaded and held in RAM.

It looks to me like the “sample” you create is only used for printing out the length. Instead, you can calculate the length from the frame length and the frame size, from the AudioInputStream and the AudioFormat.

  
    Clip clip;  // often set up as an instance variable  
    File soundFile = new File("A:/Code/MeusProjetosJava/AudioAnn/src/resources/cat10.wav");
    AudioInputStream sound = AudioSystem.getAudioInputStream(soundFile);

    // load the sound into memory (a Clip)
    DataLine.Info info = new DataLine.Info(Clip.class, sound.getFormat());
    clip = (Clip) AudioSystem.getLine(info);
    clip.open(sound);

Usually, Clips are loaded in advance of when they are to be played. At a later point, when you are ready to play them, all that is needed is the following command:


    clip.start();

Thanks for the replies.
I did a bit different

package br.lopes.audioann;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

/**
 *
 * @author AndreLopes
 */
public class Main {

    public static void main(String args[]) {

        try {
            byte[] sample = test();

        } catch (IOException | UnsupportedAudioFileException | LineUnavailableException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public static byte[] 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();

        try {
            test2(sample, format, audioInputStream.getFrameLength());
        } catch (InterruptedException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

        return sample;

    }

    public static void test2(byte[] sample, AudioFormat format, long lengthSampleFrames) throws UnsupportedAudioFileException, IOException, LineUnavailableException, InterruptedException {
        DataInputStream dis = new DataInputStream(new ByteArrayInputStream(sample));
        AudioInputStream audioInputStream = new AudioInputStream(dis, format, lengthSampleFrames);

        Clip clip = AudioSystem.getClip();
        clip.open(audioInputStream);
        clip.start();
        while (!clip.isRunning()) {
            Thread.sleep(10);
        }
        while (clip.isRunning()) {
            Thread.sleep(10);
        }

    }
    
    
 

}