0

Trying to append one .wav file to another (like the answer here Join two WAV files from Java?, but can't use AudioInputStream or AudioSystem in android). So I read you just taken the file header (44 bytes) out of the 2nd file, and concatenate that to the end of the first file to make a concatenated file, but I saw no code doing this. Do I attempted it myself and...:

public File combineWavFiles(File file1, File file2){
        byte[] bytes = new byte[(int)file2.length()];


        //compare file headers
        try {
            byte[] compBytes1 = new byte[44];
            byte[] compBytes2 = new byte[44];

            BufferedInputStream buf1 = new BufferedInputStream(new FileInputStream(file1));
            BufferedInputStream buf2 = new BufferedInputStream(new FileInputStream(file2));

            buf1.read(compBytes1, 0, 43);
            buf2.read(compBytes2, 0, 43);

            buf1.close();
            buf2.close();

            boolean equal = true;
            for (int i = 0; i < compBytes1.length; i++){
                if (compBytes1[i] != compBytes2[i]){
                    System.out.println("compBytes mismatch at "+i+": "+compBytes1[i]+" | "+compBytes2[i]);
                    equal = false;
                }
            }
            System.out.println("cWF Equal: "+equal);
        }
        catch (Exception e) {System.out.println("cWF(C) Error: "+e);}


        //read second files past 44 bytes contents
        try {
            BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file2));
            buf.read(bytes, 44, (int) file2.length()-44);
            buf.close();
        }
        catch (Exception e) {System.out.println("cWF(1) Error: "+e);}

        //write second files past 44 bytes contents to first file
        file1.setWritable(true);
        try{
            FileOutputStream output = new FileOutputStream(file1.getAbsoluteFile(), true);
            output.write(bytes);
            output.flush();
            output.close();
        }
        catch (Exception e) {System.out.println("cWF(2) Error: "+e);}

        return file1;
    }

The file does get larger, but it doesn't play. :/

Edit: after comparing file headers, it appears they do match, so mismatch there is not the issue.

1 Answers1

0

I found this: http://soundfile.sapp.org/doc/WaveFormat/ It may help to print the actual bytes and compare it with the document to make sure it fits

user
  • 1
  • 2
  • Sometimes it may help to make the array of bytes smaller and do the reading/appending in a loop. Is the file quite big? It's possible that the problem is there. Other than you should just index the array of bytes and look at the data if you didn't do it already. – user Feb 03 '19 at 07:25