将多个音频文件合并为单个音频文件

时间:2019-01-20 03:27:21

标签: android

尝试将多个音频文件合并为一个音频文件。

    FileInputStream fisToFinal = null;
    FileOutputStream fos = null;

    try {
        fos = new FileOutputStream(mergedFile);
        fisToFinal = new FileInputStream(mergedFile);
        for (File mp3File : mp3Files) {
            if (!mp3File.exists()){
                System.out.println("FILE DOESN'T EXIST!!!");
                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) {
        System.out.println("Audio Editing Error:----");
        e.printStackTrace();
    }finally {

        try {
            if (fos != null) {
                fos.flush();
                fos.close();
            }
            if (fisToFinal != null) {
                fisToFinal.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

但是,此代码仅输出第一个音频文件(但确实会使文件大小变大:/)。无法在Android中使用AudioSystem导入。

0 个答案:

没有答案