将多个.mp3文件合并为一个文件.mp3(Android)

3

我生成了许多 .mp3 文件,需要将它们合并成一个 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文件的方法,并且所有的示例都只涉及两个文件。 是否有解决我的问题的方法?

你的问题是什么,@Sameer H. Ibra? - GrIsHu
@GrIsHu:我需要一个库,可以将多个 .mp3 文件合并成一个文件吗? - Sameer H. Ibra
如果您找到了解决方案,请告知我,我也遇到了同样的问题。 - bhavesh kaila
1个回答

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接