合并文件的各个部分/以编程方式串联.wav文件

时间:2019-02-03 06:55:43

标签: java android

尝试将一个.wav文件附加到另一个文件(例如此处的答案Join two WAV files from Java?,但不能在Android中使用AudioInputStream或AudioSystem)。因此,我读到您只是从第二个文件中取出文件头(44字节),并将其连接到第一个文件的末尾以创建连接文件,但是我没有看到任何代码执行此操作。我自己尝试过吗......

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

文件确实变大了,但是无法播放。 :/

编辑:比较文件头后,看起来它们确实匹配,因此不存在匹配问题。

1 个答案:

答案 0 :(得分:0)

我发现了这一点:http://soundfile.sapp.org/doc/WaveFormat/ 可能会帮助打印实际的字节并将其与文档进行比较以确保其适合