【发布时间】:2012-06-12 06:15:38
【问题描述】:
在过去几天试图让它发挥作用时遇到了一些麻烦。但我想要的是我们有一个通过网络发送原始数据的应用程序。然后我读入这个二进制数据并想把它保存到一个 wav(任何音频)文件中。以后可能会看看压缩。
所以有问题的代码:
byte[] allBytes = ...
InputStream b_in = new ByteArrayInputStream(allBytes);
try
{
AudioFormat format = new AudioFormat(8000f, 16, 1, true, true);
AudioInputStream stream = new AudioInputStream(b_in, format, allBytes.length);
//AudioInputStream stream = AudioSystem.getAudioInputStream(b_in);
也尝试使用上述语句,但我得到了异常:javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from stream。所以我认为正在发生的事情是因为我的流是原始音频数据并且没有波头,这会引发异常?
File newPath = new File(SystemConfiguration.getLatest().voiceNetworkPathDirectory + currentPhoneCall.fileName);
if (!AudioSystem.isFileTypeSupported(Type.WAVE, stream))
{
Logger.error("Audio System file type not supported");
}
AudioSystem.write(stream, Type.WAVE, newPath);
该文件确实成功写入,但它都是静态的,我是否需要使用this 之类的东西在输出上创建一个波头。当我在记事本中查看输出的 wav 文件时,它似乎确实有一个标题,因为它以“RIFF”开头。
我需要在我的输入流中添加一个假标题吗?我应该只创建自己的输出标头并使用二进制写入器保存它吗?
【问题讨论】:
-
您的帖子有问题。您缺少数据/格式。
-
与其他有效的 WAVE 文件比较怎么样?当您选择其中之一时,将数据复制到标头之后,然后将原始数据放到代码中,并放入您的数组 allBytes。运行代码后,您可以简单地比较 2 个波形文件。如果不一样,那么要么代码不好,要么你需要更好地理解波形格式。
-
AudioInputStream的构造函数中的length参数应该是帧长度值而不是数据长度。frame-length = data-length / channel / sample-size-in-byte -
你好柳岩。我尝试设置您的帧长度,但它使情况变得更糟。它最终将除了标题之外的任何内容都放入 wav 文件中,而忽略了所有二进制数据。你介意加括号吗?是:frame-length = (data-length / channel) / sample-size-in-bytes 还是 frame-length = (data-length * sample-size-in-bytes) / channel