4

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

    }
skaffman
  • 398,947
  • 96
  • 818
  • 769
user501861
  • 51
  • 1
  • 2
  • 4
  • Sound files need to be all one format (e.g. 22500 KHz, mono, 8 bit). They also need to be the same encoding (e.g. a-law or pcm). The AudioSystem class provides ways to convert between certain types of streams/formats. Look into that. – Andrew Thompson Nov 09 '10 at 13:47
  • Even if they were all the same format, simply concatenating them won't work because wav files have a header... and concatenating them would make headers appear unexpectedly in the middle of a file. – Powerlord Nov 09 '10 at 15:45
  • 1
    He did say he skipped the header. – AHungerArtist Nov 09 '10 at 18:00
  • The WAV file has a specific format, and that format is on the web in several places. In general it's not hard to "crack", though there are several variations on the format and some are trickier than others. SMOP. – Hot Licks Aug 08 '13 at 21:27

1 Answers1

-2

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.

Community
  • 1
  • 1
Richard Miskin
  • 1,260
  • 7
  • 12
  • 1
    Thanks Richard, that may be the closest thing to an answer I can find. I keep getting confused as to the creation and placement of wav headers when splitting files. – user501861 Nov 10 '10 at 08:55
  • 1
    The linked question doesn't deal with byte arrays at all? You haven't answered the question, just told him why its not working (which he already knew). Why is this sooooo hard to find an answer to??? – CpILL Oct 04 '13 at 06:57