Java如何将音频数据存储在字节数组中. [英] Java How to store the audio data in a byte array.

查看:605
本文介绍了Java如何将音频数据存储在字节数组中.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我如何将音频文件(.au)中的音频数据读取到字节数组中?我看过有关Oracle的Java文档,但是我不知道如何使用这些信息编写程序.

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.

推荐答案

我通过音频数据"猜测,您希望从AU文件中获得音频样本,但不包括标题信息和元数据.如果只想将文件内容加载到内存中,请按照建议使用FileInputStream.

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.

否则,要阅读示例,可以使用 AudioInputStream .

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

这篇关于Java如何将音频数据存储在字节数组中.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆