如何将两个音频文件合并到android中的单个音频文件中?

时间:2015-04-16 09:43:56

标签: android audio

我希望将两个音频文件作为输入,然后按字节合并它们并将其保存为单个文件 在这段代码中,我尝试用Java编写它并且工作正常,但我不知道如何在android中完成它。

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

3 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:2)

要合并两个wav文件,请使用此代码

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

答案 2 :(得分:0)

为时已晚。但是,仍然有人可能需要适当的解决方案。这就是为什么我建议使用AudioMixer-android库的原因。您还可以执行很多音频处理任务。