转换字节数组为WAV文件 [英] Converting Byte Array to Wav file

查看:581
本文介绍了转换字节数组为WAV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

广泛研究的主题后,我已经达到了一个砖墙。

所有我想要做的就是添加的.wav文件集合到一个字节数组,一个又一个,并将其所有输出成一个完整的新创建的.wav文件。我所有的.wav数据提取到一个字节数组,跳过.WAV头和数据直行,然后当谈到其写入到新创建的.wav文件我得到这样一个错误:
ERROR1:javax.sound.sampled.UnsupportedAudioFileException:无法从输入流音频输入流
误差2:无法从输入流中获得音频输入流

在code是:

 尝试
    {
    字符串路径=*********;
    字符串路径2 =路径+newFile.wav    文件文件路径=新的文件(路径);
    文件NewfilePath =新的文件(路径2);    的String [] = folderContent filePath.list();    INT文件大小= 0;    的for(int i = 0; I< folderContent.length;我++)
        {
            RandomAccessFile的RAF =新RandomAccessFile的(路径+ folderContent [I],R);
            文件大小=文件大小+(INT)raf.length();        }    字节[] = FileBytes新的字节[文件大小]    的for(int i = 0; I< folderContent.length;我++)
        {
            RandomAccessFile的RAF =新RandomAccessFile的(路径+ folderContent [I],R);
            raf.skipBytes(44);
            raf.read(FileBytes);
            raf.close();        }    布尔成功= NewfilePath.createNewFile();    InputStream中的字节数组=新ByteArrayInputStream进行(FileBytes);    AIS的AudioInputStream = AudioSystem.getAudioInputStream(字节阵列);    AudioSystem.write(AIS,Type.WAVE,NewfilePath);    }


解决方案

您的字节数组不包含任何标题信息这可能意味着AutoSystem.write不认为这是真的WAV数据。

你能为你的组合数据创建一个适合的头?

更新:问题可能保持你的答案

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

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

    }

解决方案

Your byte array doesn't contain any header information which probably means that AutoSystem.write doesn't think it is really WAV data.

Can you create suitable header for your combined data?

Update: This question might hold the answer for you.

这篇关于转换字节数组为WAV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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