Concatenation of two WAV files failed

狂风中的少年 提交于 2019-12-12 16:30:03

问题


I have this simple code to concatenate two wav files. Its pretty simple and the code runs without any errors. But there is a problem with the output file. The output file generated does not play, and surprisingly its size is only 44 bytes whereas my input files "a.wav" & "b.wav" are both more than 500Kb in size.

Here is my code:

import java.io.File;
import java.io.IOException;
import java.io.SequenceInputStream;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;

public class WavConcat {
    public static void main(String[] args) {
        String wFile1 = "./sounds/a.wav";
        String wFile2 = "./sounds/b.wav";

        try {
            AudioInputStream clip1 = AudioSystem.getAudioInputStream(new File(wFile1));
            AudioInputStream clip2 = AudioSystem.getAudioInputStream(new File(wFile2));

            AudioInputStream appendedFiles = 
                            new AudioInputStream(
                                new SequenceInputStream(clip1, clip2),     
                                clip1.getFormat(), 
                                clip1.getFrameLength() + clip2.getFrameLength());

            AudioSystem.write(appendedFiles, 
                            AudioFileFormat.Type.WAVE,new File("./sounds/ab.wav"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

回答1:


clip1.getFormat() returns-->

MPEG2L3 24000.0 Hz, unknown bits per sample, mono, unknown frame size, 41.666668 frames/second

clip2.getFormat() returns-->

MPEG2L3 24000.0 Hz, unknown bits per sample, mono, unknown frame size, 41.666668 frames/second

That is an odd format. I can imagine the 'unknown bits per sample' is causing a problem, but also the MPEG2L3, since JavaSound has no inbuilt encoder for MP3. It seems like they are not encoded properly. Try loading them in sound editing software and save them as a type of WAV or AU that Java Sound can understand 'out of the box'. Hopefully the editing software:

  1. Can understand the broken MP3, and..
  2. Will write a valid WAV or AU.

If you can convert them to 8 bit mono & 8KHz during the conversion, it might reduce the byte[] size by a factor of 6 to 1. 8KHz is considered good enough to understand speech, and for this use you need to serve the bytes of the combined sound out to the browser - so reducing it in size is crucial.




回答2:


Try this kind of structure. This worked for me

List audioInputStreamList = new ArrayList();

    String wFile1 = "./sounds/a.wav";
    String wFile2 = "./sounds/b.wav";

 AudioInputStream audioInputStream1 = AudioSystem.getAudioInputStream(new File(wFile1));
 AudioInputStream audioInputStream2 = AudioSystem.getAudioInputStream(new File(wFile2));

 audioInputStreamList.add(audioInputStream1);
 audioInputStreamList.add(audioInputStream2);

 AudioFormat audioFormat = audioInputStream1.getFormat(); // audioInputStream2 format also same

 AudioInputStream udioInputStream = new SequenceAudioInputStream(audioFormat,audioInputStreamList);

 AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE,new File("./sounds/ab.wav"));

UPDATE

check for SequenceAudioInputStream



来源:https://stackoverflow.com/questions/13381862/concatenation-of-two-wav-files-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!