How to merge the two audio files into a single audio file in android?

后端 未结 3 1825
南旧
南旧 2020-12-30 01:47

I want to get two audio files as input, then merge them byte wise and save it as a single file.
In this code I have tried to do it in Java and it\'s working fine, but I

3条回答
  •  忘掉有多难
    2020-12-30 02:10

    For combining two wav files use this 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 WavAppender {
        public static void main(String[] args) {
            String wavFile1 = "D:\\wavOne.wav";
            String wavFile2 = "D:\\wavTwo.wav";
    
            try {
                AudioInputStream clip1 = AudioSystem.getAudioInputStream(new File(wavFile1));
                AudioInputStream clip2 = AudioSystem.getAudioInputStream(new File(wavFile2));
    
                AudioInputStream appendedFiles = 
                                new AudioInputStream(
                                    new SequenceInputStream(clip1, clip2),     
                                    clip1.getFormat(), 
                                    clip1.getFrameLength() + clip2.getFrameLength());
    
                AudioSystem.write(appendedFiles, 
                                AudioFileFormat.Type.WAVE, 
                                new File("D:\\wavAppended.wav"));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    

提交回复
热议问题