将.mp3文件合并到一个文件.mp3 Android中

时间:2013-11-25 05:56:06

标签: android merge mp3

我生成了很多.mp3文件,我需要将它合并到e mp3文件中,我试试这段代码:

FileInputStream fist = null;
    try {
        fist = new FileInputStream("/mnt/sdcard/Flash Library/Resources/sound1.mp3");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     FileInputStream fist2 = null;
    try {
        fist2 = new FileInputStream("/mnt/sdcard/Flash Library/Resources/sound2.mp3");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

        File dir = new File ("/mnt/sdcard/Flash Library/dir1");
        dir.mkdirs();

        SequenceInputStream sistream = new SequenceInputStream(fist, fist2);

        FileOutputStream fostream = null;
        try {
            fostream = new FileOutputStream("/mnt/sdcard/Flash Library/dir1/output.mp3");
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        int temp;
        try {
            while( ( temp = sistream.read() ) != -1)
            {
                Toast.makeText(ctx.getActivity(), sistream.toString(), Toast.LENGTH_SHORT).show();
                // System.out.print( (char) temp ); // to print at DOS prompt
                fostream.write(temp);   // to write to file
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            fostream.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            sistream.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            fist.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            fist2.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    return null;

首先,它会生成文件(output.mp3)有唯一的第一个mp3数据,第二个不会添加到输出,其次我需要合并许多mp3文件而不仅仅是两个? 我搜索合并mp3文件和以上示例中推荐的所有文件。 我的问题有解决办法吗?

1 个答案:

答案 0 :(得分:0)

似乎通过将内容添加到其他人的最终作品来合并.mp3文件(因为您的代码也显示了)。然后编写一个应该很容易:(未经测试)

public void mergeMp3Files(File[] inputs, File output) throws IOException {
    FileOutputStream outputStream = null;
    try {
        outputStream = new FileOutputStream(output);
        for (File input : inputs) {
            FileInputStream inputStream = null;
            try {
                IOUtils.copy(inputStream = new FileInputStream(input),
                             outputStream);
            } finally {
                if (inputStream != null) try {
                    inputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    } finally {
        if (outputStream != null) try {
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

如果您没有IOUtils.copy,请选择以下代码:

public class IOUtils {
    private static final int BUF_SIZE = 0x1000; // 4K

    public static long copy(InputStream from, OutputStream to)
            throws IOException {
        byte[] buf = new byte[BUF_SIZE];
        long total = 0;
        while (true) {
            int r = from.read(buf);
            if (r == -1) {
                break;
            }
            to.write(buf, 0, r);
            total += r;
        }
        return total;
    }
}