java 将字节数组转换为 Wav 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4133630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting Byte Array to Wav file
提问by user501861
After extensive research into the subject I have reached a brick wall.
在对该主题进行广泛研究后,我已经到达了一堵砖墙。
All I want to do is add a collection of .wav files into a byte array, one after another, and output them all into one complete newly created .wav file. I extract all the .wav data into a byte array, skipping the .wav header and going straight for the data, then when it comes to writing it to the newly created .wav file I get an error like: Error1: javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream Error2: could not get audio input stream from input stream
我想要做的就是将一组 .wav 文件一个接一个地添加到一个字节数组中,然后将它们全部输出到一个完整的新创建的 .wav 文件中。我将所有 .wav 数据提取到一个字节数组中,跳过 .wav 标头并直接获取数据,然后在将其写入新创建的 .wav 文件时出现错误,例如:Error1: javax.sound。 sampled.UnsupportedAudioFileException:无法从输入流中获取音频输入流 Error2:无法从输入流中获取音频输入流
The code is:
代码是:
try
{
String path = "*********";
String path2 = path + "newFile.wav";
File filePath = new File(path);
File NewfilePath = new File(path2);
String [] folderContent = filePath.list();
int FileSize = 0;
for(int i = 0; i < folderContent.length; i++)
{
RandomAccessFile raf = new RandomAccessFile(path + folderContent[i], "r");
FileSize = FileSize + (int)raf.length();
}
byte[] FileBytes = new byte[FileSize];
for(int i = 0; i < folderContent.length; i++)
{
RandomAccessFile raf = new RandomAccessFile(path + folderContent[i], "r");
raf.skipBytes(44);
raf.read(FileBytes);
raf.close();
}
boolean success = NewfilePath.createNewFile();
InputStream byteArray = new ByteArrayInputStream(FileBytes);
AudioInputStream ais = AudioSystem.getAudioInputStream(byteArray);
AudioSystem.write(ais, Type.WAVE, NewfilePath);
}
回答by Richard Miskin
Your byte array doesn't contain any header information which probably means that AutoSystem.write doesn't think it is really WAV data.
您的字节数组不包含任何标头信息,这可能意味着 AutoSystem.write 认为它不是真正的 WAV 数据。
Can you create suitable header for your combined data?
你能为你的组合数据创建合适的标题吗?
Update: Thisquestion might hold the answer for you.
更新:这个问题可能为你解答。