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

后端 未结 3 1777
南旧
南旧 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: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();
                }
            }
        } 
    

提交回复
热议问题