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

2019-02-15 18:58发布

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条回答
Viruses.
2楼-- · 2019-02-15 19:59

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();
}
查看更多
登录 后发表回答