Java How to store the audio data in a byte array.

2019-02-15 19:33发布

问题:

Can anyone tell me how to read store audio data from an audio file (.au) into a byte array? I've looked at the Java documentation on Oracle but I have no idea how to use the information to write a program.

回答1:

I'm guessing by 'audio data' you want the audio samples from the AU file, not including header information and metadata. If you just want to load the contents of the file into memory, use a FileInputStream as suggested.

Otherwise, to read the samples, you can use AudioSystem and AudioInputStream.

File myFile = new File("test.au");
byte[] samples;

AudioInputStream is = AudioSystem.getAudioInputStream(myFile);
DataInputStream dis = new DataInputStream(is);      //So we can use readFully()
try
{
    AudioFormat format = is.getFormat();
    samples = new byte[(int)(is.getFrameLength() * format.getFrameSize())];
    dis.readFully(samples);
}
finally
{
    dis.close();
}