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

后端 未结 3 1776
南旧
南旧 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();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-30 02:17

    It is too late. But still, someone might need a proper solution. That is why I am suggesting using AudioMixer-android library. You can also perform a lot of audio processing things.

    0 讨论(0)
  • 2020-12-30 02:18
    private void mergeSongs(File mergedFile,File...mp3Files){
            FileInputStream fisToFinal = null;
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(mergedFile);
                fisToFinal = new FileInputStream(mergedFile);
                for(File mp3File:mp3Files){
                    if(!mp3File.exists())
                        continue;
                    FileInputStream fisSong = new FileInputStream(mp3File);
                    SequenceInputStream sis = new SequenceInputStream(fisToFinal, fisSong);
                    byte[] buf = new byte[1024];
                    try {
                        for (int readNum; (readNum = fisSong.read(buf)) != -1;)
                            fos.write(buf, 0, readNum);
                    } finally {
                        if(fisSong!=null){
                            fisSong.close();
                        }
                        if(sis!=null){
                            sis.close();
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                try {
                    if(fos!=null){
                        fos.flush();
                        fos.close();
                    }
                    if(fisToFinal!=null){
                        fisToFinal.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } 
    
    0 讨论(0)
提交回复
热议问题