Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

audio

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();
        }
        }
}
like image 980
Srinivas Keerthiprakasam Avatar asked Apr 16 '15 09:04

Srinivas Keerthiprakasam


People also ask

Can I combine two audio files?

You can combine hundreds of audios, from MP3, WAV, M4A, M4R, FLAC, OGG, DTS, WMA, and many rare formats into one track. No matter it is for combining audiobooks, voice recordings and songs, you can batch import the files into VideoProc Converter, with option to adjust their order easily and visually.


2 Answers

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();
            }
        }
    } 
like image 139
Srinivas Keerthiprakasam Avatar answered Sep 19 '22 18:09

Srinivas Keerthiprakasam


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();
        }
    }
}
like image 35
Sunisha Sindhu Avatar answered Sep 20 '22 18:09

Sunisha Sindhu