2

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.

Estra
  • 155
  • 1
  • 3
  • 10

1 Answers1

3

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();
}
prunge
  • 22,460
  • 3
  • 73
  • 80
  • Hello I've tried using the above code but I keep getting "Exception in thread "main" java.io.FileNotFoundException: auSample.au (The system cannot find the file specified)" though I have the audio file in the correct folder. Are these changes I need to make to the code or is it simply just me linking the file incorrectly. – Estra Nov 04 '12 at 22:04
  • Try using absolute path in the file name. Also, you need to escape any backslashes in the file name if the file name is a string literal. e.g. `new File("c:\\temp\\sample.au");` – prunge Nov 05 '12 at 02:30
  • Hello I did try absolute path but it did not work for some reason. I've used ClassLoader.getResource(); and it now works. Thanks for the help – Estra Nov 06 '12 at 00:47