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();
}