保存声音的byte []对wav文件 [英] Saving audio byte[] to a wav file

查看:1331
本文介绍了保存声音的byte []对wav文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一直有一个有点麻烦,在过去几天试图让这个工作。但是,我要的是我们有通过网络发送原始数据的应用程序。然后我读这个二进制数据,并希望将其保存为WAV(任何音频)文件。不妨来看看COM pression更高版本。

Been having a bit of trouble over the last few days trying to get this to work. But what I want is we have an application that sends raw data over the network. I then read this binary data in and want to save it to a wav(any audio) file. Might look at compression later.

于是问题code:

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:无法从流音频输入流。因此,我认为正在发生的是,因为我流是原始的音频数据,并没有一个波头这是抛出一个异常?

Have tried to use the above statement as well but i get the exception: javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from stream. So what I think is happening is that because my stream is raw audio data and does not have a wave header this is throwing an exception?

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

该文件不写入成功,但它是所有静态的,我需要创建使用类似的这个。当我看到在记事本中输出的wav文件和它似乎有一个头,因为它与RIFF开始。

The file does successfully write but it is all static, Do I need to create a wave header on the output using the something like this. When I look at the outputted wav file in notepad and it does seem to have a header as it starts with 'RIFF'.

我需要一个假头加入到我的输入流?我是不是应该创建自己的输出头,只是保存了二进制的作家?

Do I need to add a fake header into my input stream? Should i just create my own output header and just save it with a binary writer?

推荐答案

这是很难从你的描述中错误的是知道的,但要回答你的问题,当你看到RIFF当你打开文件,它意味着它有一个头。试图建立自己的,你的头是可能的,但有点耗费时间(和不必要的,因为你有一个)。

It's hard to know from your description where the error is, but to answer your questions, when you see 'RIFF' when you open the file that does mean it has a header. Trying to build you own header is possible but a bit time consuming (and unnecessary since you've got one).

现在,读书的时候,它是最安全的从文件本身得到的格式,而不是试图手动指定它 - 这将是不容易出错。有样品code为这里:

Now, when reading, it is safest to get the format from the file itself, rather than trying to specify it manually -- that will be less error prone. There is sample code for that here:

http://www.jsresources.org/examples/SimpleAudioPlayer.java.html

写作时,你说:该文件没有写成功,但它是所有静态的 - 你确认,比自己的Java code以外的东西?也许我不理解正确,但它听起来就像你正试图记录和新鲜的java code播放WAV文件,如果是这样的话,这是很难说,如果事情是与记录或播放脚麻。

When writing, you say "The file does successfully write but it is all static" -- have you confirmed that with something other than your own java code? Maybe I am not understanding correctly, but it sounds like you are trying to record and playback WAV files with fresh java code, and if that's the case, it's hard to tell if something is going wrong with the recording or the playback.

您应该以香草格式开始(16位,立体声,uncom pressed,44,100千赫),所以你可以打开Media Player或QuickTime或一些文件,并确认记录工作。一旦这样的作品,你可以尝试改变SR等。下面是一些示例code进行录制。开始与,验证它工作,然后转移到更复杂的东西:

You should start with a vanilla format (16 bits, stereo, uncompressed, 44,100 kHz) so you can open the file in media player or quicktime or something and verify that the recording worked. Once that works, you can try to change the SR and so on. Here is some sample code for recording. Start with that, verify that it works, and then move on to something more complex:

http://www.jsresources.org/examples/SimpleAudioRecorder.java.html

这篇关于保存声音的byte []对wav文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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