如何在android中合并两个音频mp3文件?

时间:2014-05-28 08:59:54

标签: android audio merge

我想合并两个录制的音频文件。好吧,到目前为止我在发布之前搜索了很多。 假设我将first.mp3和second.mp3合并为merged.mp3,那么merged.mp3没有second.mp3的数据 它与first.mp3相同。

这是我的代码片段

MainActivity Snippets

Handler handler=new Handler();
                  handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {

                        File first =  new File(path1);
                        File second = new File(path2);
                        String filepath = Environment.getExternalStorageDirectory().getPath();
                        File file = new File(filepath, "Dictatapp");
                        mergeSongs(new File(file.getAbsoluteFile()+"/mergedFile.mp3"), new File[] {first, second});

                    }
                },2000);

合并功能

private void mergeSongs(File mergedFile,File...mp3Files){

    try {
        FileInputStream fis1 = new FileInputStream(mp3Files[0]);
        FileInputStream fis2 = new FileInputStream(mp3Files[1]);
        Log.i("merge",mp3Files[0]+" "+mp3Files[1]);
        Toast.makeText(MainActivity.this, mp3Files[0].toString()+mp3Files[1].toString(), Toast.LENGTH_LONG).show();
        SequenceInputStream sis = new SequenceInputStream(fis1,fis2);

        if(!mergedFile.exists()){
            mergedFile.createNewFile();}

        FileOutputStream fos = new FileOutputStream(mergedFile);

        int temp;

         while ((temp = sis.read())!= -1){

                fos.write((byte)temp);

            }

         fis1.close();
         fis2.close();
         sis.close();
         fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

}

0 个答案:

没有答案