简体   繁体   中英

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

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 don't know how to do it in android.

How to do it in android?

import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;

public class FileMixer {
    public static void main(String[] args)
    {
        try
        {
    Path path1 = Paths.get("C:\\Srini\\Wav\\welcome.wav");
    Path path2 = Paths.get("C:\\Srini\\Wav\\goodbye.wav"); 
    String path3 ="C:\\Srini\\Wav\\srini12.wav";
    File Newfilepath=new File(path3);

    byte[] byte1 = Files.readAllBytes(path1);
    byte[] byte2 = Files.readAllBytes(path2);
    byte[] out = new byte[byte1.length];
    for (int i=0; i<byte1.length; i++)
    {
        out[i] = (byte) ((byte1[i] + byte2[i]) >> 1);
    }

    InputStream byteArray = new ByteArrayInputStream(out);
    AudioInputStream ais = AudioSystem.getAudioInputStream(byteArray);
    AudioSystem.write(ais, AudioFileFormat.Type.WAVE,Newfilepath);
    }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        }
}
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();
            }
        }
    } 

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM